3

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?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Randomblue
  • 112,777
  • 145
  • 353
  • 547

3 Answers3

2

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/

timoxley
  • 5,156
  • 3
  • 36
  • 40
  • You said: "I'd bet most people don't even know the selector parameter is there." How is someone supposed to know about the "selector" parameter, when the official documentation at http://api.jquery.com/live/ shows the following syntax for .live()?: A) .live( events, handler ) B) .live( events [, data ], handler ) C) .live( events ). The selector parameter does not appear anywhere in the syntax from the official documentation. – Jaime Montoya Sep 13 '17 at 18:22
1

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
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • I think the 4th argument of live is the context. So actually you can attach the event to a more specific place than the document level. – Randomblue Sep 05 '11 at 00:13
  • @Randomblue I do not understand why a "4th argument" would be allowed when according to the syntax that I see at http://api.jquery.com/live/, the maximum number of arguments that are allowed is three, which is what I see for added from version 1.4: .live( events [, data ], handler ). – Jaime Montoya Sep 13 '17 at 20:13
0

A general answer to grasp the fundamental idea is summarized at https://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/:

The jQuery team have announced in v1.7 a new method for binding events called on. This method combines the functionality of live, bind, and delegate described below, allowing you to specify the binding method based the arguments passed rather than by using different function names.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103