Search code examples
javascriptjqueryevent-delegation

difference between jQuery.live and jQuery.delegate


I have read some post about why do not use jQuery.live() and I want to check if I got it:) When I call $("body").delegate('element','click', function);

Is it the same as $(element).live('click', function) ? In case of normal behaviour..According to the post there are some stopPropagation and performance boons, but is the main difference that live bind everytime to body element, while delegate can bind to another one?


Solution

  • One important difference is that ".live()" will actually build up the jQuery element list for the initial selector, even though the ".live()" function itself only needs the selector string. That means that if the selector is somewhat expensive, the code to set up the handler will go running all over the DOM for no good reason. The ".delegate()" call does not do that.

    Really I don't see any reason that new code should use ".live()"; it was sort-of an architectural mistake and should eventually die quietly.