jQuery Tutorial Tutorials - jQuery .delegate() method

jQuery .delegate() method

The .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().

jQuery .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

jQuery .delegate() method Example

example

when the user clicks any paragraph in the div, shows its text contents as an alert.

$( "div" ).delegate( "p", "click", function() {
  alert( $( this ).text() );
});

Try now

example

attach multiple events

$('body').delegate('#foo','mouseenter mouseleave', function() {
  $(this).toggleClass('entered');
});

Try now

Date:2019-08-29 23:41:01 From:www.Lautturi.com author:Lautturi