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 8160709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:20:08+00:00 2026-06-06T18:20:08+00:00

Consider a basic addEventListener as window.onload=function(){ document.getElementById(alert) .addEventListener(‘click’, function(){ alert(OK); }, false); } where

  • 0

Consider a basic addEventListener as

window.onload=function(){
  document.getElementById("alert")
  .addEventListener('click', function(){
     alert("OK");
  }, false);
}

where <div id="alert">ALERT</div> does not exist in the original document and we call it from an external source by AJAX. How we can force addEventListener to work for newly added elements to the documents (after initial scan of DOM elements by window.onload)?

In jQuery, we do this by live() or delegate(); but how we can do this with addEventListener in pure Javascript? As a matter of fact, I am looking for the equivalent to delegate(), as live() attaches the event to the root document; I wish to make a fresh event listening at the level of parent.

  • 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-06-06T18:20:09+00:00Added an answer on June 6, 2026 at 6:20 pm

    Overly simplified and is very far away from jQuery’s event system but the basic idea is there.

    http://jsfiddle.net/fJzBL/

    var div = document.createElement("div"),
        prefix = ["moz","webkit","ms","o"].filter(function(prefix){
             return prefix+"MatchesSelector" in div;
        })[0] + "MatchesSelector";
    
    Element.prototype.addDelegateListener = function( type, selector, fn ) {
    
        this.addEventListener( type, function(e){
            var target = e.target;
    
            while( target && target !== this && !target[prefix](selector) ) {
                target = target.parentNode;
            }
    
            if( target && target !== this ) {
                return fn.call( target, e );
            }
    
        }, false );
    };
    

    What you are missing on with this:

    • Performance optimizations, every delegate listener will run a full loop so if you add many on a single element, you will run all these loops
    • Writable event object. So you cannot fix e.currentTarget which is very important since this is usually used as a reference to some instance
    • There is no data store implementation so there is no good way to remove the handlers unless you make the functions manually everytime
    • Only bubbling events are supported, so no "change" or "submit" etc which you took for granted with jQuery
    • Many others which I’m simply forgetting about for now
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a basic Rails development pipeline, going from development -> staging -> production. When
Consider I have a game-creation framework, with a basic class called Game. // Action
This is a very basic question, so please bear with me. Consider the following
Let's consider this basic insert insert into TableName (Col1,Col2,Col3) values (Val1,Val2,Val3) i want this
consider the following basic class layout: public class Base : IComparable<Base> { public int
I've got some basic questions about C++. Consider the following code in which I
For ease of discussion, consider this basic table (Test) in Access... ID division name
Consider: char [] chararray = txt1.Text; How we do the same in Visual Basic
In this scenario (your basic catalog), there are multiple <div> s, each containing an
I have a basic question about the form action. Consider the below code here,

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.