I’m seeing that more developers are now using $('#element-id').on() method instead of $('#element-id').live() method.
Why? What is has that live method doesn’t have?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Event handlers bound with
.liveare bound to thedocument. Event handlers bound with.onare bound to an element you specify. That means you can reduce the amount of time taken for the event to bubble up to the element that will receive it, and it also means you can stop the propagation (not possible with.livesince it’s already reached the document).The
.onmethod also allows you to use a single method for all event handlers –.livewill always delegate the event handler, whereas.onwill only delegate if you pass a selector as the 2nd argument. Without one, it will bind to the matched set of elements.More drawbacks are listed in the jQuery docs:
For all of these reasons, you should always use
.on()instead of.live(). If you’re stuck on an old version of jQuery (below 1.7), you can use.delegate()instead.Side note – all event binding methods call
.on()under the hood, so it makes sense to use.on()for all your event handling needs. Don’t bother with the shortcut methods like.click(); you can see from the 1.7.2 source that all they do is call.on: