The live()
method was deprecated in jQuery 1.7. The jQuery docs now recommend
Use
.on()
to attach event handlers. Users of older versions of jQuery should use.delegate()
in preference to.live()
.
I understand how on
and delegate
work, but I don't understand why they are better. live()
is simpler and easier to use.
Is there a reason why live
was deprecated? How are the other methods better? Will anything bad happen if I continue to use live
?
jQuery live() Method The live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).
In short: . bind() will only apply to the items you currently have selected in your jQuery object. . live() will apply to all current matching elements, as well as any you might add in the future. The underlying difference between them is that live() makes use of event bubbling.
on() method: This method attaches events not only to existing elements but also for the ones appended in the future as well. The difference here between on() and live() function is that on() method is still supported and uses a different syntax pattern, unlike the above two methods.
The live () method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live () method will work for both current and FUTURE elements matching the selector (like a new element created by a script).
As of jQuery 1.4 the .live () method supports custom events as well as all JavaScript events that bubble. It also supports certain events that don't bubble, including change, submit, focus and blur.
The live () method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live () method will work for both current and FUTURE elements matching the selector (like a new element created by a script). Tip: To remove event handlers, use the die () method.
This method is used to attach one or more event handlers for the selected elements. It also specifies the function that runs when the event occurs. The event handlers used will work for both current and future elements matching the selector. Event: It is used to specify events, which will be attached to the elements.
See some of the explanations here:
http://www.ultimatewebtips.com/why-jquery-live-is-a-bad-option-to-use/(Site appears to be down)
Quote:
You can’t use .live()
for reusable widgets.
.stopPropagation()
doesn’t work with live.
.live()
is slower.
.live()
is not chainable.
Further beauty of .on()
is that it streamlines all events quite well: http://api.jquery.com/on/
D'uh you know about the api link and see how .on()
works :)
Quote:
The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live(). To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With