Advertisement
  1. Code
  2. Web Development

Quick Tip: The Difference Between Live() and Delegate()

Scroll to top
Read Time: 2 min

In jQuery 1.3, the team introduced the live() method, which allows us to bind event handlers to elements on the page, as well as any that might be created in the future dynamically. Though not perfect, it definitely proved to be helpful. Most notably, live() bubbles all the way up, and attaches the handler to the document. It also ceases to work well when chaining method calls, unfortunately. Delegate() was introduced in version 1.4, which almost does the same thing, but more efficiently.

We'll examine the specific differences between the two methods in today's video quick tip. Thanks to the FireQuery Firebug extension, we'll have the tools to more easily understand how each method functions.

Alternate Viewing Options

1
2
<ul id="items">
3
	<li> Click Me </li>
4
</ul>
1
2
// Bind attaches an event handler only to the elements

3
// that match a particular selector. This, expectedly,

4
// excludes any dynamically generated elements.

5
$("#items li").click(function() {
6
	$(this).parent().append("<li>New Element</li>");
7
});
8
9
10
// Live(), introduced in 1.3, allows for the binding

11
// of event handlers to all elements that match a 

12
// selector, including those created in the future.

13
// It does this by attaching the handler to the document.

14
// Unfortunately, it does not work well with chaining.

15
// Don't expect to chain live() after calls like 

16
// children().next()...etc. 

17
$("li").live("click", function() {
18
	$(this).parent().append("<li>New Element</li>");
19
});	
20
21
22
// Delegate, new to version 1.4, perhaps should have been a complete 

23
// replacement for Live(). However, that obviously

24
// would have broken a lot of code! Nonetheless, 

25
// delegate remedies many of the short-comings

26
// found in live(). It attaches the event handler

27
// directly to the context, rather than the document. 

28
// It also doesn't suffer from the chaining issues

29
// that live does. There are many performance benefits

30
// to using this method over live().

31
$('#items').delegate('li', 'click', function() {
32
	$(this).parent().append('<li>New Element</li>');
33
});	
34
35
36
// By passing a DOM element as the context of our selector, we can make

37
// Live() behave (almost) the same way that delegate()

38
// does. It attaches the handler to the context, not

39
// the document - which is the default context.

40
// The code below is equivalent to the delegate() version

41
// shown above.

42
$("li", $("#items")[0]).live("click", function() {
43
	$(this).parent().append("<li>New Element</li>");
44
});

Conclusion

This can definitely be a confusing topic. Please feel free to ask questions, or discuss within the comments. Thanks so much to Elijah Manor for clarifying a few things for me on this topic!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.