16

I love the new live event in jQuery 1.3. The question I have is the performance of this event. I know the advantages of using live over click/bind('click') but is there a performance hit for using it over click/bind('click')?

If not, why would you ever use click or bind('click')?

Mike
  • 2,716
  • 4
  • 26
  • 27
  • Dup: http://stackoverflow.com/questions/748076/using-live-benefits-similar-to-bind – Crescent Fresh Sep 02 '09 at 15:06
  • I think they are different questions as this one relates solely to performance differences and not in general. – MacAnthony Sep 02 '09 at 15:18
  • @MacAnthony: read the accepted answer to that question, it delves into the performance characteristics of `live` and `bind`. That's why I Dup'd this: it has been answered already. – Crescent Fresh Sep 02 '09 at 16:21
  • Cool, thanks for the link. Didn't seem to find it when searching earlier. – Mike Sep 02 '09 at 20:12

2 Answers2

12

If not, why would you ever use click or bind('click')?

Because $.live() has some significant disadvantages

  • Live events do not bubble in the traditional manner and cannot be stopped using stopPropagation (This changed in jquery 1.4.4) or stopImmediatePropagation. For example, take the case of two click events - one bound to "li" and another "li a". Should a click occur on the inner anchor BOTH events will be triggered. This is because when a $("li").bind("click", fn); is bound you're actually saying "Whenever a click event occurs on an LI element - or inside an LI element - trigger this click event." To stop further processing for a live event, fn must return false.
  • Live events currently only work when used against a selector. For example, this would work: $("li a").live(...) but this would not: $("a", someElement).live(...) and neither would this: $("a").parent().live(...).
weexpectedTHIS
  • 3,358
  • 1
  • 25
  • 30
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
2

See this.

As for why you would ever use click or bind instead of live, the answer is because you don't need the extra functionality.

code_burgar
  • 12,025
  • 4
  • 35
  • 53