why do programmers say that “live” is inefficient?
- So what are the alternative
methods to replicating that function
that are more efficient? - How do we measure the impact of how much it slows things down?
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.
I suppose it is inefficient because the handler is placed at the root node, and relies on bubbling to catch the event and run the proper handler.
One alternative would be to simply
bindyour handler to your dynamically created elements when they are created and added to the DOM.Another alternative is to bind a single handler to a container, and let your events bubble up to that. This can be nice if you have lots of identical elements added to a container.
Bind a click handler to
#myContainerinstead of each.myElement.I image this may suffer from some of the same inefficiencies as
.live(), but ought to be better as it is more localized. New.myElementitems added, automatically work.EDIT:
According to the docs: As of jQuery 1.4, event bubbling can optionally stop at a DOM element “context”.
This would seem to create a similar effect to the last method I mentioned.
EDIT:
As suggested by Nick Craver, jQuery’s
.delegate()method can produce a similar effect more cleanly.Example courtesy of Nick: