Click Switch (jQuery)

$("li").hover( function () { // do x }, function () { // do y }); 

.. thats for hover, how would I do the same for switching clicks, i.e. x on click and y on second click?

Manythanks

+4
source share
3 answers
 $('li').toggle(function() { alert(1) }, function() { alert(2); }); 
+11
source

$.toggle() will perform any number of functions. Here it is with two:

 $("li").toggle( function() { /* do x */ }, function() { /* do y */ } ); 

Demo: http://jsfiddle.net/vbkBQ/

+11
source

One problem with toggle () is that it automatically calls event.preventDefault() . Here is one way that will allow you to leave the default action in place or only allow it to be called conditionally.

 $("a").click(function(event){ var toggle = ($(this).data('toggle') == undefined) ? true : $(this).data('toggle'); console.log(toggle); if(toggle){ event.preventDefault(); } $(this).data('toggle', !toggle); });​​ 
0
source

All Articles