How can I store which events were removed with unbind and re-apply them later?
Suppose that I have this element:
<div id="thediv">The Div</div>
Which has a varying number of functions attached to its onclick event. I know I could use unbind to remove all the onclick functions:
$("#thediv").unbind("click");
How can I store the unbound functions so as to re-bind them later?
Note that this must work with jQuery 1.5.
I did see this previous answer , but there a couple of things I didn't understand about it:
- Why is it binding first and then unbinding?
- What is
ary_handlers[idx]doing?
(I'm not really looking for answers to these questions, unless they are necessary to explain the solution for my question about capturing the unbound functions.)
I think you could do something like this: you store the events of the div by cloning the div and saving data('events') in an object. Afterwords you iterate on the object and bind back the events. You have to clone because when you unbind the events the original data('events') are deleted.(hope i understood what you are looking for)
fidlle http://jsfiddle.net/pXAXW/
EDIT - To make this work in 1.5.2 you just need to change the way you attach back the events because they are saved differently:
fiddle here: (the same as Katiek) http://jsfiddle.net/nicolapeluchetti/CruMx/2/ (the event is triggered twice if you don't click exactly on the div!) I also updated my fiddle to use jquery 1.5.2 http://jsfiddle.net/pXAXW/1/)