Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery on('toggle') possible?

Tags:

jquery

toggle

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

I would need to use the .on() method on the element I want to use .toggle() on but I can't figure out how to specify my second event handler function:

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

Where would I have to specify the second handler function for my toggle event?

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

like image 865
Vincent Avatar asked Jul 20 '12 12:07

Vincent


1 Answers

Try using only the toggle.

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

As it is, it doesn't do anything on the first click because it's only attaching the event handler at that point.

EDIT:

I didn't fully read the post. 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;
    }
});
like image 136
M. Cesar Avatar answered Oct 21 '22 09:10

M. Cesar