mitomed
mitomed

Reputation: 2066

Jquery .off all handlers

Maybe something relevant that I'm missing but if I use this when document ready

$('a.edit').on("click", displayEditForm);

And then this after an ajax call

$('a.edit').off();
$('a.edit').on('click', displayEditForm);

It works, I need to deattach because some of the a.click are dynamically loaded and some aren't so I want to attach only once to all of them. Problem is that this works for now but I would like to use

$('a.edit').off('click', displayEditForm);
$('a.edit').on('click', displayEditForm);

and that's not working. It doesn't fail but some of them are calling the function twice when clicked. Doesn't work neither if I use

$('a.edit').off('click', '**');

Any idea why?

Thanks

Upvotes: 1

Views: 121

Answers (1)

steo
steo

Reputation: 4656

If you are generating them dynamically you can bind the click() like these :

$(document).on('click' , 'a.edit', function(){})

or use $(document).delegate('a.edit', 'click', function(){})

Personally I use the first solution

Upvotes: 7

Related Questions