Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:00:52+00:00 2026-05-29T08:00:52+00:00

How to apply live() like feature for JavaScript appended DOM elements? Like a li

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:00:53+00:00Added an answer on May 29, 2026 at 8:00 am

    Since .live() is simply event delegation, place your handler on the nearest element to the ones being added.

    var container = document.getElementById('my_container');
    
    container.onclick = function(e) {
        e = e || window.event;
        var target = e.target || e.srcElement;
    
        while(target && target.nodeName.toUpperCase() !== 'LI' ) {
            if( target === this )
                target = null;
            else 
                target = target.parentNode;
        }
    
        if( target ) {
            // work with the LI
        }
    };
    

    This is also similar to .live() in the sense that it searches from the e.target up to the container with the delegate to see if it is your targeted element.

    Just testing the e.target itself isn’t enough if the li has descendants.


    For more complex analysis of the elements, you could use .matchesSelector, though you’d need to stick it on the HTMLElement.prototype under the correct name, since most browsers include it as an extension.

    Also, you’d need a patch for IE8, but that’s pretty easy.

    if (HTMLElement) {
        if (!HTMLElement.prototype.matches && !HTMLElement.prototype.matchesSelector) {
            HTMLElement.prototype.matches =
            HTMLELement.prototype.matchesSelector = 
                HTMLElement.prototype.webkitMatchesSelector ||
                HTMLElement.prototype.mozMatchesSelecvtor ||
                HTMLElement.prototype.msMatchesSelector ||
                HTMLElement.prototype.oMatchesSelector;
        }
    } else if (!Element.prototype.matchesSelector && Element.prototype.querySelectorAll) {
    
        Element.prototype.matches = 
        Element.prototype.matchesSelector =
            function() {
                // exercise for reader to implement using .querySelectorAll, 
                //    though it's pretty easy, and available online if you search
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question would probably apply equally as well to other languages with C-like multi-line
I have a text like this: My name is Bob and I live in
This use case seems like it would apply to just about every form, but
As mentioned i would like to know how i can apply the live() function
I need to apply this script to work the same way for two other
i apply this method to a <textarea></textarea> element but i would like to return
I need to apply some xml templates to various streams of xml data (and
I would like to run some code right after a certain set of elements
I am creating an li element dynamically like this: $(data).find('slide').each(function(numSlide){ var slideLink = $(this).attr('link');
If I have a HTML element <header> and apply some margins to this HTML5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.