Is there a way I can use the ‘bind’ syntax for the ‘toggle’ event handler ?
From the docs I can understand that the correct way is
$('.selector').toggle( function() {}, function() {} );
My problem is that I am removing that element for some reason, and then again add it to the DOM. On adding it again, the toggle does not work.
Thus, I think there should be a way to ‘bind’ the toggle.
Help please.
This is documentation I am following:
http://jqapi.com/#p=toggle
You must use either
.live()or.delegate()to add handlers to all now and future elements.The problem is that
.toggle()binds click, and in complicated cases, like using.live()or.delegate()you have to make your own. From the jQuery Docs:Roll your own toggle (this can be easily extended to N toggled functions using mod N instead of mod 2 – but then you have to use a switch instead of the JS conditional operator):
If you want to include 2 functions, then they must be self executing. This would be good for passing arguments in… so the form would be:
A simple example (no arguments used):
try it out on jsFiddle
A more complex example (arguments used):
try it out on jsFiddle