Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between .delegate() and .live()

Tags:

jquery

So I was looking at the jQuery source and I found this:

delegate: function( selector, types, data, fn ) {
    return this.live( types, data, fn, selector );
},

So .delegate() function is pretty much just the .live() function. The only difference is the order of the arguments! Why would the jQuery people do such a thing?

like image 908
Randomblue Avatar asked Apr 24 '26 20:04

Randomblue


2 Answers

People generally omit the selector parameter on live, I'd bet most people don't even know the selector parameter is there.

delegate() gives you easy access to the selector parameter, which allows you to 'scope' your event listener to just a subset of the whole dom, which can result in better performance.

It's awkward to provide additional parameters after passing an inline anonymous function. Since the selector parameter is so useful, it makes sense for jquery to create a more convenient form.

See: http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/

like image 171
timoxley Avatar answered Apr 27 '26 09:04

timoxley


They are exactly the same except:

  1. .delegate() lets you narrow down event handling on a certain element, while .live() must process events in the entire page because `live attaches the event handler at the document level.
  2. .live() takes advantage of the event bubbling from element until the document where as delegate stops at the element on which delegate is applied.
  3. Using delegate you have a very good control on the event handling as compared to live
like image 28
ShankarSangoli Avatar answered Apr 27 '26 08:04

ShankarSangoli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!