Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Unbind event handlers to bind them again later

Does anybody know how to unbind set of event handlers, but memorize them in order to bind them again later? Any suggestions?

like image 614
glaz666 Avatar asked Feb 05 '09 15:02

glaz666


People also ask

Does jQuery remove unbind events?

jQuery unbind() MethodThe unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs.

Can event handlers be removed?

Go to the objects tab, view the Document object (don't click on edit) and scroll down to Event Handlers. Select the one to delete and press delete.

What is bind and unbind in jQuery?

jQuery bind() function is used to attach an event handler to elements, while the unbind() is used to detached an existing event handler from elements.


2 Answers

There is a events element in the data of the item. This should get your started, you can read your elements and store the handlers in an array before unbinding. Comment if you need more help. I got this idea from reading the $.fn.clone method so take a look at that as well.

$(document).ready(function() {     $('#test').click(function(e) {         alert('test');         var events = $('#test').data("events");         $('#test').unbind('click', events.click[0]);     }); });  <a id="test">test</a> 
like image 67
bendewey Avatar answered Oct 17 '22 20:10

bendewey


Here is how to achieve that, provides a storeEvents and a restoreEvents methods on a selection. storeEvents takes a snapshot of the events on the moment it is called. restoreEvents restores to the last previous snapshot. Might need to twist it a little for parameterizing the unbinding while restoring, maybe you'd like to keep the bound events after the last snapshot.

(function($){      function obj_copy(obj){             var out = {};         for (i in obj) {             if (typeof obj[i] == 'object') {                 out[i] = this.copy(obj[i]);             }             else                 out[i] = obj[i];         }         return out;     }       $.fn.extend({          storeEvents:function(){             this.each(function(){                 $.data(this,'storedEvents',obj_copy($(this).data('events')));             });             return this;         },          restoreEvents:function(){             this.each(function(){                 var events = $.data(this,'storedEvents');                 if (events){                     $(this).unbind();                     for (var type in events){                         for (var handler in events[type]){                             $.event.add(                                 this,                                  type,                                  events[type][handler],                                  events[type][handler].data);                         }                     }                 }             });             return this;         }      }); })(jQuery); 
like image 32
glmxndr Avatar answered Oct 17 '22 19:10

glmxndr