Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery live() vs delegate() [duplicate]

Tags:

jquery

live

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

like image 596
PeeHaa Avatar asked Jan 02 '11 15:01

PeeHaa


People also ask

What is the difference between bind() vs live() vs delegate() methods in jQuery?

The bind() method will not attach events to those elements which are added after DOM is loaded while live() and delegate() methods attach events to the future elements also. The difference between live() and delegate() methods is live() function will not work in chaining.

What is delegate in jQuery?

The delegate() method attaches one or more event handlers for specified elements that are children of selected elements, and specifies a function to run when the events occur. Event handlers attached using the delegate() method will work for both current and FUTURE elements (like a new element created by a script).


2 Answers

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() { ... }); 
like image 99
lonesomeday Avatar answered Sep 20 '22 01:09

lonesomeday


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

like image 27
user113716 Avatar answered Sep 22 '22 01:09

user113716