Returns: jQuery
.delegate(selector, eventType, handler)
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.
.delegate(selector, eventType, eventData, handler)
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.
eventData A map of data that will be passed to the event handler.
handler A function to execute at the time the event is triggered.
.delegate(selector, events)
selector A selector to filter the elements that trigger the event.
events A map of one or more event types and functions to execute for them.
Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.
Delegate is an alternative to using the .live() method, allowing for each binding of event delegation to specific DOM elements. For example the following delegate code:
$("table").delegate("td", "hover", function(){
$(this).toggleClass("hover");
});
Is equivalent to the following code written using .live():
$("table").each(function(){
$("td", this).live("hover", function(){
$(this).toggleClass("hover");
});
});
See also the .undelegate() method for a way of removing event handlers added in .delegate().
Passing and handling event data works the same way as it does for .bind().
Example Demo < 1 >
Click a paragraph to add another. Note that .delegate() binds the click event to all paragraphs - even new ones.
Click me!
Comments