Categories: jQuery Events > Event Handling
Description: Remove a handler from the event for all elements which match the current selector, now or in the future, based upon a specific set of root elements.
.undelegate( )
version added: 1.4.2
.undelegate( selector, eventType )
version added: 1.4.2
selector
A selector to filter the elements that trigger the event.
eventType
A string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.
.undelegate( selector, eventType, handler )
version added: 1.4.2
selector
A selector to filter the elements that trigger the event.
eventType
A string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.
handler
A function to execute at the time the event is triggered.
.undelegate( selector, events )
version added: 1.4.3
selector
A selector which will be used to filter the event results.
events
A map of one or more event types and previously bound functions to unbind from them.
.undelegate( namespace )
version added: 1.6
namespace
A string containing a namespace to unbind all events from.
Undelegate is a way of removing event handlers that have been bound using .delegate(). It works virtually identically to .die() with the addition of a selector filter argument (which is required for delegation to work).
Examples
Example 1
Can bind and unbind events to the colored button.
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("body").delegate("#theone", "click", aClick)
.find("#theone").text("Can Click!");
});
$("#unbind").click(function () {
$("body").undelegate("#theone", "click", aClick)
.find("#theone").text("Does nothing...");
});
Example 1 - Full source:
Can bind and unbind events to the colored button.<!DOCTYPE html>
<html>
<head>
<style>
button { margin:5px; }
button#theone { color:red; background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("body").delegate("#theone", "click", aClick)
.find("#theone").text("Can Click!");
});
$("#unbind").click(function () {
$("body").undelegate("#theone", "click", aClick)
.find("#theone").text("Does nothing...");
});
});
</script>
</head>
<body>
<button id="theone">Does nothing...</button>
<button id="bind">Bind Click</button>
<button id="unbind">Unbind Click</button>
<div style="display:none;">Click!</div>
</body>
</html>
Example 2
To unbind all delegated events from all paragraphs, write:
$("p").undelegate()
Example 3
To unbind all delegated click events from all paragraphs, write:
$("p").undelegate( "click" )
Example 4
To undelegate just one previously bound handler, pass the function in as the third argument:
var foo = function () {
// code to handle some kind of event
};
$("body").delegate("p", "click", foo); // ... now foo will be called when paragraphs are clicked ...
$("body").undelegate("p", "click", foo); // ... foo will no longer be called.
Example 5
To unbind all delegated events by their namespace:var foo = function () {
// code to handle some kind of event
};
// delegate events under the ".whatever" namespace
$("form").delegate("click.whatever", ":button", foo);
$("form").delegate("keypress.whatever", ":text", foo);
// unbind all events delegated under the ".whatever" namespace
$("form").undelegate(".whatever");
Was this information helpful?

