I have a div with id="menu" in my index.html file and I want to hide it using jQuery. In my script.js I wrote:
window.onload = function() { prepareEventHandlers(); } function prepareEventHandlers() { var hideDiv = $("#menu"); var crtButton = $("#hide"); crtButton.onclick = function() { hideDiv.hide(); }; } But when I press the button on the webpage the div is still there, the only way that works is if in the index.html I write:
<button type="button" onclick="$('#menu').hide()" id="hide">HideDiv</button> but I don't want to use it since it is bad style, so how can I make this work using my script.js code?
crtButtonis a jQuery object. You can either use his code or in your code usecrtButton[0].onclick