$(document).ready(function () { $("#menu").hover( function () { $('#hide').show(); // #hide = display:none; }, function () { $('#hide').hide(); } ); }); 

If you just write $ ("# menu"). Click or $ ("# menu"). OnClick, nothing happens.

  • @ Alexey Shimansky, if toggle -> images disappear <a id='menu'> <img> </a> - ALPHA

3 answers 3

Use toggle :

 $(document).ready(function () { $("#menu").click(function() { $('#hide').toggle() }); }); 
  • It is necessary that when the buttons are turned on, the other included buttons are turned off. And bind the close button id = "close" (which is in the window that changes the display from none to block). - ALPHA
  • @ALPHA, what is your class of buttons? Do you want one block to open when pressed, while others hide? - Yuri
  • <a id='menu'> <img> </a> - several such button links, when you click on the "#" link, js changes display none to the block of the block described above. - ALPHA
  • @ ALPHA, well? #menu on #menu will open / hide the block - Yuri
  • other windows open too and the intermittent ones do not close. - ALPHA

I decided all pure JS `th! It is not clear why jquery was invented.

 function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } 

In html write so:

 <a href="#" onclick="toggle_visibility('foo');"> <div id="foo">This is foo</div> 
  • JQuery simplifies work, my friend :) - Yuri

So?

 $(document).ready(function () { $("#menu").on('click', function() { function () { $('#hide').show(); // #hide = display:none; }, function () { $('#hide').hide(); } }); }); 

This is if you match the question itself. Hovering replaced by a click, if you want to create a state replacement, then you need toggle, as other commentators said.

  • SyntaxError: function statement requires a name - ALPHA
  • @ALPHA your problem solved by the user "Yuri"? If yes - mark his answer as correct, if not explain, please, again what you want to get as a result of the script. I just showed how to assign handlers through certain events, but your code (which I simply took from your question) is incorrect, because the script will not work in any case until your code is fixed. You wrote the question “how to replace hover by click” in the subject line, but it seems that your function itself is incorrect and therefore the question needs to be reformulated. - Telion