Felix Engelmann
Felix Engelmann

Reputation: 1

.delegate works, .live doesn't. Why?

Can someone explain this? My code works properly, now that i use .delegerate but i have no idea why I have to use it. I have a ajax created element with a class of "ajaxDiv". When I use .live like this I don't get an alert. All the code I wrote after this function won't work either.

$(".ajaxDiv").live("click", function(event){
    alert('I don\'t work.')
});

But when I do it like this, it work's.

$("body").delegate(".ajaxDiv", "click", function(){
    alert('I work like a charm!')
});

Thanks!

Upvotes: 0

Views: 77

Answers (3)

Freeman Lambda
Freeman Lambda

Reputation: 3675

Live has been deprecated in jQuery.

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live(). Reference: http://api.jquery.com/live/

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191819

.live was removed in jQuery 1.9. .delegate has not been removed yet. In fact, it is not deprecated, but its use has been superseded by .on (.delegate documentation). I would suggest that you use .on for all bindings including event delegation.

Upvotes: 0

TwiN
TwiN

Reputation: 1911

I guess you are using jQuery 1.9? .live() is removed.

At the meantime, you can use the Migrate Plugin to restore .live() or continue using .delegate().

However, for best forward compatibility, consider using .on(). Simply switch the first 2 arguments of .delegate()

Upvotes: 3

Related Questions