Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with the jQuery live method?

Tags:

jquery

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?

like image 611
Peter Olson Avatar asked Jun 20 '12 08:06

Peter Olson


People also ask

What is jQuery 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).

What is difference between BIND and live in jQuery?

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.

What is the difference between on and live in jQuery?

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.

What is the use of the live () method in jQuery?

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).

What types of events does jQuery live () support?

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.

What is the purpose of the 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). Tip: To remove event handlers, use the die () method.

What is the use of event method in jQuery?

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.


1 Answers

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:

  1. You can’t use .live() for reusable widgets.

  2. .stopPropagation() doesn’t work with live.

  3. .live() is slower.

  4. .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()

like image 64
Tats_innit Avatar answered Sep 21 '22 13:09

Tats_innit