A repository of over 1000 quality jQuery plugins

jQuery .delegate()

Learn all about the jQuery function .delegate().

As of jQuery 1.7, .delegate() has been superseded by the .on() method. For earlier versions, however, it remains the most effective means to use event delegation. More information on event binding and delegation is in the .on() method. In general, these are the equivalent templates for the two methods:

1
2
3
4
// jQuery 1.4.3+
$( elements ).delegate( selector, events, data, handler );
// jQuery 1.7+
$( elements ).on( events, selector, data, handler );

For example, the following .delegate() code:

1
2
3
$( "table" ).delegate( "td", "click", function() {
$( this ).toggleClass( "chosen" );
});

is equivalent to the following code written using .on():

1
2
3
$( "table" ).on( "click", "td", function() {
$( this ).toggleClass( "chosen" );
});

To remove events attached with delegate(), see the .undelegate() method.

Passing and handling event data works the same way as it does for .on().