51

I want to know the main difference between

.live() vs. .bind()

methods in jQuery.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • 15
    You and a lot of other people too: http://stackoverflow.com/questions/937039/what-is-the-difference-between-the-bind-and-live-methods-in-jquery – peirix Apr 22 '10 at 11:49
  • 5
    Also worth mentioning is that since 1.7 (released after this question) `live()` is deprecated in favor of `on()`. – J.G.Sebring Dec 19 '12 at 09:45
  • For an e.g. `$(document).on("click", "input#goodbye", function(){ alert("Goodbye!"); });` and dont pace it in $(document).ready(); keep it outside of it. at same level. – Rajan Rawal Mar 15 '13 at 03:56

4 Answers4

94

The main difference is that live will work also for the elements that will be created after the page has been loaded (i.e. by your javascript code), while bind will only bind event handlers for currently existing items.

// BIND example
$('div').bind('mouseover', doSomething);
// this new div WILL NOT HAVE mouseover event handler registered
$('<div/>').appendTo('div:last');

// LIVE example
$('div').live('mouseover', doSomething);
// this new appended div WILL HAVE mouseover event handler registered
$('<div/>').appendTo('div:last');

Update:

jQuery 1.7 deprecated live() method and 1.9 has removed it. If you want to achieve the same functionality with 1.9+ you need to use a new method on() which has slightly different syntax as it's invoked on document object and the selector is passed as a parameter. Therefor the code from above converted to this new way of binding events will look like this:

// ON example
$(document).on('mouseover', 'div', doSomething);
// this new appended div WILL HAVE mouseover event handler registered
$('<div/>').appendTo('div:last');
RaYell
  • 69,610
  • 20
  • 126
  • 152
27

I did a statistical analysis of .bind() vs .live() vs .delegate() using FF profiler. I did 10 rounds of each (not a sufficient sample to be definitive, but illustrates the point). These are the results.

1) Single static element with an id using the click event:

.bind():     Mean = 1.139ms, Variance = 0.1276ms
.live():     Mean = 1.344ms, Variance = 0.2403ms
.delegate(): Mean = 1.290ms, Variance = 0.4417ms

2) Multiple static elements with a common class using the click event:

.bind():     Mean = 1.089ms, Variance = 0.1202ms
.live():     Mean = 1.559ms, Variance = 0.1777ms
.delegate(): Mean = 1.397ms, Variance = 0.3146ms

3) Multiple dynamic elements (first button creates second...) using the click event:

.bind():     Mean = 2.4205ms, Variance = 0.7736ms
.live():     Mean = 2.3667ms, Variance = 0.7667ms
.delegate(): Mean = 2.1901ms, Variance = 0.2838ms

Interpret how you wish, but it seems to me that as dynamic elements increase on a page, .delegate() seems to have the best performance while static elements perform best with .bind().

Keep in mind that I am using a very simple click event triggering an alert. Different pages, with different environments (ie. CPU, multi-tab browsing, running threads, etc) will render differing results. I used this as a basic guideline for my decision to use one or the other. Please advise if you have come up with a different result.

Thanks!

Watermark Studios
  • 1,173
  • 1
  • 10
  • 24
18

You should consider to use .delegate() instead of .live() whereever possible. Since event delegation for .live() always targets the body/document and you're able to limit "bubbling" with .delegate().

See http://api.jquery.com

UPDATE

From jQuery:

As of jQuery 1.7, .delegate() has been superseded by the .on() method. For earlier versions, however, .delegate() remains the most effective means to use event delegation.

LCJ
  • 22,196
  • 67
  • 260
  • 418
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • See my comment below, but I must generally agree. If the tree structure of a page is unknown (and you don't want to bind an event to ``) .live() may be a better catch-all, but I see that being more the exception than the rule. – Watermark Studios Oct 07 '11 at 20:41
  • 3
    it appears that from jquery 1.7 onward, the preferred method to use is .on() – Syg Jan 25 '12 at 16:02
11

As of v1.7, .live, .bind, and .delegate have all been replaced by .on http://api.jquery.com/on/

I was curious of the diffence myself, so I wrote up an article with some code examples. http://blog.tivix.com/2012/06/29/jquery-event-binding-methods/.

Sounds like depending upon how you call .on(), jquery will mimic .bind, .live, or .delegate. This gives your event handlers a more elegant implementation.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
andyzinsser
  • 2,463
  • 2
  • 18
  • 18
  • the given link is dead. For those interested in migrating your code: https://web.archive.org/web/20121004004902/http://blog.tivix.com/2012/06/29/jquery-event-binding-methods/ – Aaron_H Dec 30 '15 at 02:57