A plain object of one or more event types and functions to execute for them.

As of jQuery 3.0, .delegate() has been deprecated. It was superseded by the .on() method since jQuery 1.7, so its use was already discouraged. 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().

Additional Notes:

Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will propagate to the elements to which they are delegated; event handlers bound on any elements below it in the DOM tree will already have been executed by the time the delegated event handler is called. These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false.

Examples:

Click a paragraph to add another. Note that .delegate() attaches a click event handler to all paragraphs - even new ones.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<!doctype html>

<htmllang="en">

<head>

<metacharset="utf-8">

<title>delegate demo</title>

<style>

p{

background: yellow;

font-weight: bold;

cursor: pointer;

padding:5px;

}

p.over{

background:#ccc;

}

span{

color: red;

}

</style>

<scriptsrc="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>

<body>

<p>Click me!</p>

<span></span>

<script>

$( "body" ).delegate( "p", "click", function() {

$( this ).after( "<p>Another paragraph!</p>" );

});

</script>

</body>

</html>

Demo:

To display each paragraph's text in an alert box whenever it is clicked:

1

2

3

$( "body" ).delegate( "p", "click", function() {

alert( $( this ).text() );

});

To cancel a default action and prevent it from bubbling up, return false: