What is the difference between the bind and live methods in jQuery?

JqueryBindLive

Jquery Problem Overview


I'm curious to know the differences between the bind and live functions.

To me they seem to be almost identical.

I read the benefits of live/bind methods, but it didn't tell me about the differences...

Thanks!

Jquery Solutions


Solution 1 - Jquery

In short: .bind() will only apply to the items you currently have selected in your jQuery object. .live() will apply to all current matching elements, as well as any you might add in the future.

The underlying difference between them is that live() makes use of event bubbling. That is, when you click on a button, that button might exist in a <p>, in a <div>, in a <body> element; so in effect, you're actually clicking on all of those elements at the same time.

live() works by attaching your event handler to the document, not to the element. When you click on that button, as illustrated before, the document receives the same click event. It then looks back up the line of elements targeted by the event and checks to see if any of them match your query.

The outcome of this is twofold: firstly, it means that you don't have to continue reapplying events to new elements, since they'll be implicitly added when the event happens. However, more importantly (depending on your situation), it means that your code is much much lighter! If you have 50 <img> tags on the page and you run this code:

$('img').click(function() { /* doSomething */ });

...then that function is copied into each of those elements. However, if you had this code:

$('img').live('click', function() { /* doSomething */ });

...then that function is stored only in one place (on the document), and is applied to whatever matches your query at event time.

Because of this bubbling behaviour though, not all events can be handled this way. As Ichiban noted, these supported events are click, dblclick mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup.

Solution 2 - Jquery

.bind() attacheds events to elements that exist or match the selector at the time the call is made. Any elements created afterwards or that match going forward because the class was changed, will not fire the bound event.

.live() works for existing and future matching elements. Before jQuery 1.4 this was limited to the following events: click, dblclick mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup

Solution 3 - Jquery

Bind will bind events to the specified pattern, for all matches in the current DOM at the time you call it. Live will bind events to the specified pattern for the current DOM and to future matches in the DOM, even if it changes.

For example, if you bind $("div").bind("hover", ...) it will apply to all "div"s in the DOM at the time. If you then manipulate the DOM and add an extra "div", it won't have that hover event bound. Using live instead of bind would dispatch the event to the new div as well.

Solution 4 - Jquery

Nice read on this: http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/

Is nowadays (since jQuery 1.7) deprecated using the .on() function - http://api.jquery.com/on/

Solution 5 - Jquery

imagine this scenario:

  1. i have several <img> elements.
  • $('img').bind('click', function(){...});
  • add some extra images (using get(), or html(), anything)
  • the new images don't have any binding!!

of course, since the new images didn't exist when you did the $('img')... at step 2, it didn't bind the event handler to them.

now, if you do this:

  1. i have several <img> elements.
  • $('img').live('click', function(){...});
  • add some extra images (using get(), or html(), anything)
  • the new images do have the binding!!

magic? just a little. in fact jQuery binds a generic event handler to another element higher in the DOM tree (body? document? no idea) and lets the event bubble up. when it gets to the generic handler, it checks if it matches your live() events and if so, they're fired, no matter if the element was created before or after the live() call.

Solution 6 - Jquery

In adition to what they said, I think it's best to try to stick to bind when/where you can and use live only when you must.

Solution 7 - Jquery

All these jQuery methods are used for attaching events to selectors or elements. But they all are different from each other.

.bind(): This is the easiest and quick method to bind events. But the issue with bind() is that it doesn’t work for elements added dynamically that matches the same selector. bind() only attach events to the current elements not future element. Above that it also has performance issues when dealing with a large selection.

.live(): This method overcomes the disadvantage of bind(). It works for dynamically added elements or future elements. Because of its poor performance on large pages, this method is deprecated as of jQuery 1.7 and you should stop using it. Chaining is not properly supported using this method.

Find out more here

Solution 8 - Jquery

I wanted to add to this after having to debug a bit due to my own silliness. I applied .live() to a class of button on my page, assuming that it would just render out the correct ID I was trying to pass on the query string and do what I wanted to do with the ajax call. My app has dynamically added buttons associated with an inventory item. For instance, drill down categories to the 'COKE' button to add a coke to your order. Drill down from the top again, and add 'BUDLITE' - each time I wanted those items to be entered into a table via an AJAX call.

However, since I bound .live() to the entire class of buttons, it would remember each ajax call I had made and re-fire it for each subsequent button! It was a little tricky because I wasn't exactly clear on the difference between bind and live (and the answer above is crystal about it), so I figured I'd put this here just in case somebody was doing a search on this stuff.

Solution 9 - Jquery

There is a way to get the live effect but its kind of nasty.

$(this).unbind('mouseout').bind('mouseout',function(){ });

this will clear the previous and reset the new. It has seemed to work fine for me over time.

Solution 10 - Jquery

Difference between live and livequery is discussed here .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin BrownView Question on Stackoverflow
Solution 1 - JquerynickfView Answer on Stackoverflow
Solution 2 - JqueryichibanView Answer on Stackoverflow
Solution 3 - JqueryRob BeardowView Answer on Stackoverflow
Solution 4 - JqueryJeff MaesView Answer on Stackoverflow
Solution 5 - JqueryJavierView Answer on Stackoverflow
Solution 6 - JqueryandiView Answer on Stackoverflow
Solution 7 - JqueryRahul KumarView Answer on Stackoverflow
Solution 8 - JqueryChrisMView Answer on Stackoverflow
Solution 9 - JqueryDavidView Answer on Stackoverflow
Solution 10 - JqueryRogerView Answer on Stackoverflow