Is jQuery on ('toggle') possible?

Instead of using $('.element').toggle(function(){}, function(){});

I need to use the .on() method for the element I want to use .toggle() , but I cannot figure out how to specify my second event handler function:

 $('body').on('toggle', '.element', function(){}); 

Where do I need to specify the function of the second handler for my switch event?

I did it here: http://jsfiddle.net/D33f4/ , but nothing happens on the first click. I really don't understand why.

+6
source share
1 answer

Try using only the switch.

 $('#clickme').toggle( function(){alert('click 1');}, function(){alert('click 2');} ); 

Be that as it may, it does nothing at the first click, because it only binds the event handler to this point.

EDIT:

I did not fully read the message. My bad.

 $('body').on('click', '#clickme', function() { var state = $(this).data('state'); switch(state){ case 1 : case undefined : alert('click 1'); $(this).data('state', 2); break; case 2 : alert('click 2'); $(this).data('state', 1); break; } }); 
+6
source

Source: https://habr.com/ru/post/920963/


All Articles