Deregistering Event Handlers

After registering an event handler with bind() (or with any of the simpler event registration methods) you can deregister it with unbind() to prevent it from being triggered by future events. (Note that unbind() only deregisters event handlers registered with bind() and related jQuery methods. It does not deregister handlers passed to addEventListener() or the IE method attachEvent(), and it does not remove handlers defined by element attributes such as onclick and onmouseover.) With no arguments, unbind() deregisters all event handlers (for all event types) for all elements in the jQuery object:

// Remove all jQuery event handlers from all elements!
$('*').unbind();  

With one string argument, all handlers for the named event type (or types, if the string names more than one) are unbound from all elements in the jQuery object:

// Unbind all mouseover and mouseout handlers
// of all <a> tags
$('a').unbind("mouseover mouseout"); 

This is a heavy-handed approach and should not be used in modular code because someone might also be using other modules that register their own handlers for the same event types on the same elements. If your module registered event handlers using namespaces, however, you can use this one-argument version of unbind() to deregister only the handlers in your namespace:

// Unbind all mouseover and mouseout handlers
// in the "myMod" namespace
$('a').unbind("mouseover.myMod mouseout.myMod"); 
// Unbind handlers for any event in the myMod namespace
$('a').unbind(".myMod");
// Unbind click handlers that are in both namespaces
$('a').unbind("click.ns1.ns2");

If you want to be careful to unbind only event handlers you registered yourself, and you did not use namespaces, you must retain a reference to the event handler functions and use the two-argument version of unbind(). In this form, the first argument is an event type string (without namespaces), and the second argument is a handler function:

$('#mybutton').unbind('click', myClickHandler);

When invoked this way, unbind() deregisters the specified event handler function for events of the specified type (or types) from all elements in the jQuery object. Note that event handlers can be unbound using this two-argument version of unbind(), even when they were registered with an extra data value using the three-argument version of bind().

You can also pass a single object argument to unbind(). In this case, unbind() is invoked recursively for each property of the object. The property name is used as the event type string, and the property value is used as the handler function:

$('a').unbind({  // Remove specific event handlers
   mouseover: mouseoverHandler,
   mouseout: mouseoutHandler
});

Finally, there is one more way that unbind() can be invoked. If you pass a jQuery Event object to it, it unbinds the event handler to which that event was passed. Calling unbind(ev) is equivalent to unbind(ev.type, ev.handler).

Get jQuery Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.