0

After asking this question : jQuery die() does not work. I found that live() does not seem to behave like bind().

I had the following line:

$('.produit').die().live('change',function(){ // the rest
$('.produit').live('change',function(){ // that did not work either

Then I changed it to:

$('.produit').unbind('change').bind('change',function(){ // the rest

What is the difference between the two lines.

In this example .produit is added dynamically to the page. And the binding is done after the prepend().

I'm using jQuery 1.4.2, and IE7.

Community
  • 1
  • 1
David Laberge
  • 15,435
  • 14
  • 53
  • 83
  • 2
    p.s. you should *really* consider upgrading jQuery if at all possible. You'll find it is very good at backwards compatibility. [1.4.2 is olddddd](http://blog.jquery.com/2010/02/19/jquery-142-released/) – Matt Nov 07 '11 at 15:37

2 Answers2

2

If you use IE there is some problem with live and change event

search for livequery plugin which solves this.

try to change the event to Click event and youll see that it works.

The difference is that Bind is for Already In Page elements and live is also+Future elements.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • This one http://stackoverflow.com/questions/1637503/jquery-change-event-on-select-not-firing-in-ie – Royi Namir Nov 07 '11 at 15:29
  • there is no unbind for Live - its Unbind is Die. – Royi Namir Nov 07 '11 at 15:32
  • @RoyiNamir: That was probably in an older version of jQuery. The latest versions cover up IE's shortcomings seamlessly. – Matt Nov 07 '11 at 15:33
  • 1
    @Matt I know but hes using old ver. ( I had this problem on 1.3.2) I dont know if it fixed on 1.4 – Royi Namir Nov 07 '11 at 15:34
  • This is a very vague answer. It would be better to explain that the `change` event doesn't bubble in IE so `live`, which binds to a parent element and then triggers the event with the proper context, never receives word that the `change` event occurred. If `livequery` solves this, an example and/or link would be a great way to really hit it home. – coreyward Nov 07 '11 at 15:34
  • @Royi So the `change` event presumably doesn't bubble up the DOM tree in IE... As a result, event delegation (which `live` is) won't work. – Šime Vidas Nov 07 '11 at 15:35
  • @coreyward `live` binds at the `document` object, not the parent element. – Šime Vidas Nov 07 '11 at 15:36
  • @ŠimeVidas if you want to bind direct to the parent you should use delegate – Royi Namir Nov 07 '11 at 15:37
  • @ŠimeVidas Yes, and it is the parent of everything on the page. Point is the bubbling has to occur or it won't be notified. `.live` is also deprecated and David should be using `.delegate` instead. – coreyward Nov 07 '11 at 15:40
  • @coreyward `document` is the *ancestor* of every element, not the parent. – Šime Vidas Nov 07 '11 at 15:41
  • @coreyward Actually, jQuery is pushing `.on()` and `.off()` since version 1.7, so `.delegate()` is kind-of deprecated, too. – Šime Vidas Nov 07 '11 at 15:42
  • @ŠimeVidas True, but he's using 1.4, not 1.7. – coreyward Nov 07 '11 at 15:43
2

Live does not behave like bind. That is correct.

Live attaches a handler for only predefined actions (like click or keypress). With bind you can define your own events and trigger them however you deem necessary.

All in all, in the end, it is better to use bind over live. That is why in the newest jQuery 1.7 (which you are not using) there is the functions on and off which basically combines the functionality of live, bind, and delagate

Naftali
  • 144,921
  • 39
  • 244
  • 303