102

I've read some posts here and elsewhere on the web about the differences between live() and delegate(). However I haven't found the answer I'm looking for (if this is a dupe please tell me).

I know that the difference between live and delegate is that live cannot be used in a chain. I also read somewhere that delegate is in some cases faster (better performance).

My question is, is there a situation where you should use live instead of delegate?

UPDATE

I've set up a simple test to see the difference in performance.

I've also added the new .on() which is available in jQuery 1.7+

The results pretty much sum up the performance issues as stated in the answers.

  • Don't use .live() unless your jQuery version doesn't support .delegate().
  • Don't use .delegate() unless your jQuery version doesn't support .on().

The difference between .live() and .delegate() is A LOT bigger than between delegate() and .on().

lukeocom
  • 3,243
  • 3
  • 18
  • 30
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I took the liberty of summarising this excellent post and other useful comments in one place, for my own record and in case this anyone else finds it useful. [**JQuery live vs delegate vs bind**](http://www.timacheson.com/Blog/2011/oct/jquery_live_vs_delegate) – Tim Nov 02 '11 at 10:52
  • As of version 1.7, `.live()` is gone. You will find the new `.on()` method instead. – Qun Wang Aug 07 '12 at 03:09

4 Answers4

148

I never use live; I consider the benefits of using delegate to be so substantial as to be overwhelming.

The one benefit of live is that its syntax is very close to that of bind:

$('a.myClass').live('click', function() { ... });

delegate, however, uses a slightly more verbose syntax:

$('#containerElement').delegate('a.myClass', 'click', function() { ... });

This, however, seems to me to be much more explicit about what is actually happening. You don't realise from the live example that the events are actually being captured on document; with delegate, it is clear that the event capturing happens on #containerElement. You can do the same thing with live, but the syntax becomes increasingly horrid.

Specifying a context for your events to be captured also improves performance. With the live example, every single click on the entire document has to be compared with the selector a.myClass to see if it matches. With delegate, that is only the elements within #containerElement. This will obviously improve performance.

Finally, live requires that your browser looks for a.myClass whether or not it currently exists. delegate only looks for the elements when the events are triggered, giving a further performance advantage.


NB delegate uses live behind the scenes, so you can do anything with live that you can do with delegate. My answer deals with them as they are commonly used.

Note also that neither live nor delegate is the best way to do event delegation in modern jQuery. The new syntax (as of jQuery 1.7) is with the on function. The syntax is as follows:

$('#containerElement').on('click', 'a.myClass', function() { ... });
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • 4
    +1. The re-use of a preceding selector in `live()` is confusing, leading to user misunderstanding of what it's actually doing. `delegate()` is clearer. Also, though `delegate()` is (currently) implemented using `live()`, it's using a four-argument version of `live()` that isn't documented, so presumably for internal use only. Personally I'd always avoid `live()` unless I needed to run on jQuery 1.3 for some reason (hopefully not). – bobince Jan 02 '11 at 16:10
  • 4
    @all: From the jQuery docs: `As of jQuery 1.4, live events can be bound to a DOM element "context" rather than to the default document root.` Wouldn't `live()` be better to use now since we can add the event context. Since `delegate()` calls `live()` internally. So I think 1 call less. Or am I wrong? – PeeHaa May 16 '11 at 17:55
  • Great answer. Really great. However, I still prefer live. unless my application is one huge monstrous js app, the simplicity of live is greater than the performance gain. Moreover, since I will usually be using bind (or one of it's shorter versions), the closeness is great. Still a really great answer. Made me actaully get the difference. +1. – frostymarvelous Oct 04 '11 at 11:38
  • @PeeHaa I think you're right about that, but as mentioned before you, using delegate makes things easier to read. – trusktr Dec 17 '11 at 00:46
  • @trusktr actually I couldn't be more wrong. See my updated question for a perf test. – PeeHaa Dec 17 '11 at 01:26
  • is this a new kind of war, event propagation approach holy war? – Luka Ramishvili Mar 15 '12 at 08:07
24

They are exactly the same except:

  • .delegate() lets you narrow down the a local section of the page, while .live() must process events in the entire page.
  • .live() starts with a wasted DOM selection

When you call .delegate(), it just turns around and calls .live(), but passes the extra context parameter.

https://github.com/jquery/jquery/blob/master/src/event.js#L948-950

As such, I'd always use .delegate(). If you really need for it to process all events on the page, then just give it the body as the context.

$(document.body).delegate('.someClass', 'click', function() {
    // run handler
});

Older versions of jQuery actually have delegate functionality. You just need to pass a selector or element as the context property when calling .live(). Of course, it needs to be loaded on the page.

$('.someClass', '#someContainer').live('click',function() {
    // run handler
});

And you have the same behavior as .delegate().

user113716
  • 318,772
  • 63
  • 451
  • 440
3

Two situations come to mind:

  1. You would be using delegate on the body element, so then you can just use live instead as it's simpler.

  2. You need to use an older version of the jQuery library, where the delegate event is not yet implemented.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Consider this example

<ul id="items">  
   <li> Click Me </li>  
</ul>

$('#items').delegate('li', 'click', function() {  
    $(this).parent().append('<li>New Element</li>');  
});

By passing a DOM element as the context of our selector, we can make Live() behave (almost) the same way that delegate() does. It attaches the handler to the context, not the document - which is the default context. The code below is equivalent to the delegate() version shown above.

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

Resource

But, you'd better use delegate for better performance see here

Misagh Aghakhani
  • 1,023
  • 2
  • 13
  • 29