How to apply live() like feature for JavaScript appended DOM elements?
Like a li list inside ul which is added through JavaScript. I need to do this in plain JavaScript.
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.
Since
.live()is simply event delegation, place your handler on the nearest element to the ones being added.This is also similar to
.live()in the sense that it searches from thee.targetup to the container with the delegate to see if it is your targeted element.Just testing the
e.targetitself isn’t enough if thelihas descendants.For more complex analysis of the elements, you could use
.matchesSelector, though you’d need to stick it on theHTMLElement.prototypeunder the correct name, since most browsers include it as an extension.Also, you’d need a patch for IE8, but that’s pretty easy.