Saving a jQuery event handler for later use

Quick note: If you need to save an event handler so you can unbind the event and rebind it later, the code below will do the trick. This is especially helpful if you’re using a jQuery plugin that attaches events to your element, but you want to temporarily unbind it.

var submit = $("#submit");
//Save original click event handler for later
var click = submit.data("events")["click"][0].handler;
//Unbind click event
submit.unbind("click");
//Bind click to original event
submit.click(function (e) {
	$.proxy(click, submit)(e);
});
Advertisements