Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
101
rated 0 times [  103] [ 2]  / answers: 1 / hits: 38901  / 14 Years ago, tue, january 4, 2011, 12:00:00

Using jQuery, I would like to, without changing the disabled attribute of a given button, disable all click events for it.



I was thinking of retrieving the click event handlers, unbind them, and storing them (say, using data()).



Then I can re-bind them once the button is enabled again.


More From » jquery

 Answers
71

Not hard to do since jQuery already stores all of its event handlers as data() on the element itself. You can get (and modify) this object through .data().events.



Now you can easily save a reference to the handlers with:



 events._click = events.click;
events.click = null;


And then restore them by using:



 events.click = events._click;
events._click = null;


Note that this won't disable events that are bound via .delegate() or .live(), as they work by event bubbling/propagation. To disable those as well, simply bind a new handler that blocks propagation to ancestor elements:



 events._click = events.click;
events.click = null;
// Block .live() and .delegate()
$(#el).click(function (e) {
e.stopPropagation();
});


You don't even need to unbind this blocker function when it's time to enable the handlers again, since events.click = events._click will override the function you just bound with all the old handlers.


[#94387] Sunday, January 2, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
adrienkeithr

Total Points: 503
Total Questions: 126
Total Answers: 110

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
;