.delegate()
methodThe .delegate()
method in jQuery has been deprecated. Use the .on
method instead.It was used to attach hanlders to elements events,now or in the future based on the set of root elements.
Use .undelegate()
to remove the handler previously attached by .delegate()
.
.delegate()
method Syntax.delegate(selector,eventType,handler) .delegate(selector,eventType,eventData,handler) .delegate(selector,events)
Parameter | Type | Description |
---|---|---|
selector |
String | filter the elements that trigger the event. |
eventType |
String | event Type such as 'click','submit' |
handler |
Function | event handler function |
eventData |
Anything | the data passed to event handler |
events |
Object | contains event type and function handler |
.delegate()
method Examplewhen the user clicks any paragraph in the div, shows its text contents as an alert.
$( "div" ).delegate( "p", "click", function() { alert( $( this ).text() ); });
attach multiple events
$('body').delegate('#foo','mouseenter mouseleave', function() { $(this).toggleClass('entered'); });