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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:30:23+00:00 2026-05-14T15:30:23+00:00

What behavior should I expect if I delete a DOM element that was used

  • 0

What behavior should I expect if I delete a DOM element that was used to start an event bubble, or whose child started the event bubble – will it continue to bubble if the element is removed?

For example – lets say you have a table, and want to detect click events on the table cells. Another piece of JS has executed an AJAX request that will eventually replace the table, in full, once the request is complete.

What happens if I click the table, and immediately after the table gets replaced by a successful completion of an AJAX request? I ask because I am seeing some behavior where the click events don’t seem to be bubbling – but it is hard to duplicate.

I am watching the event on a parent element of the table (instead of attaching the event to every TD), and it just doesn’t seem to reach it sometimes.

EDIT: Encountered this problem again, and finally got to the root of it. Was not a event-bubbling issue at all! See my answer below for details.

  • 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-14T15:30:23+00:00Added an answer on May 14, 2026 at 3:30 pm

    Empirically: It depends on what browser you’re using; IE cancels the event, everything else (as far as I can tell) continues it. See the test pages and discussion below.

    Theoretically: Andy E’s head helpfully found that DOM2 says the event should continue because bubbling should be based on the initial state of the tree. So the behavior of the majority is correct, IE’s on its own here. Quelle surprise.

    But: Whether that relates to what you’re seeing is another question indeed. You’re watching for clicks on a parent element of the table, and what you suspect is that very rarely, when you click the table, there’s a race condition with an Ajax completion that replaces the table and the click gets lost. That race condition can’t exist within the Javascript interpreter because for now, Javascript on browsers is single-threaded. (Worker threads are coming, though — whoo hoo!) But in theory, the click could happen and get queued by a non-Javascript UI thread in the browser, then the ajax could complete and replace the element, and then the queued UI event gets processed and doesn’t happen at all or doesn’t bubble because the element no longer has a parent, having been removed. Whether that can actually happen will depend a lot on the browser implementation. If you’re seeing it on any open source browsers, you might look at their source for queuing up UI events for processing by the interpreter. But that’s a different matter than actually removing the element with code within the event handler as I have below.

    Empirical results for the does-bubbling-continue aspect:

    Tested Chrome 4 and Safari 4 (e.g., WebKit), Opera 10.51, Firefox 3.6, IE6, IE7, and IE8. IE was the only one that cancelled the event when you removed the element (and did so consistently across versions), none of the others did. Doesn’t seem to matter whether you’re using DOM0 handlers or more modern ones.

    UPDATE:
    On testing, IE9 and IE10 continue the event, so IE noncompliance with spec stops at IE8.

    Test page using DOM0 handlers:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Test Page</title>
    <style type='text/css'>
    body {
        font-family: sans-serif;
    }
    #log p {
        margin:     0;
        padding:    0;
    }
    </style>
    <script type='text/javascript'>
    window.onload = pageInit;
    
    function pageInit() {
        var parent, child;
    
        parent = document.getElementById('parent');
        parent.onclick = parentClickDOM0;
        child = document.getElementById('child');
        child.onclick = childClickDOM0;
    }
    
    function parentClickDOM0(event) {
        var element;
        event = event || window.event;
        element = event.srcElement || event.target;
        log("Parent click DOM0, target id = " + element.id);
    }
    
    function childClickDOM0(event) {
        log("Child click DOM0, removing");
        this.parentNode.removeChild(this);
    }
    
    function go() {
    }
    
    var write = log;
    function log(msg) {
        var log = document.getElementById('log');
        var p = document.createElement('p');
        p.innerHTML = msg;
        log.appendChild(p);
    }
    
    </script>
    </head>
    <body><div>
    <div id='parent'><div id='child'>click here</div></div>
    <hr>
    <div id='log'></div>
    </div></body>
    </html>
    

    Test page using attachEvent/addEventListener handlers (via Prototype):

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Test Page</title>
    <style type='text/css'>
    body {
        font-family: sans-serif;
    }
    #log p {
        margin:     0;
        padding:    0;
    }
    </style>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js'></script>
    <script type='text/javascript'>
    document.observe('dom:loaded', pageInit);
    function pageInit() {
        var parent, child;
    
        parent = $('parent');
        parent.observe('click', parentClick);
        child = $('child');
        child.observe('click', childClick);
    }
    
    function parentClick(event) {
        log("Parent click, target id = " + event.findElement().id);
    }
    
    function childClick(event) {
        log("Child click, removing");
        this.remove();
    }
    
    function go() {
    }
    
    var write = log;
    function log(msg) {
        $('log').appendChild(new Element('p').update(msg));
    }
    </script>
    </head>
    <body><div>
    <div id='parent'><div id='child'>click here</div></div>
    <hr>
    <div id='log'></div>
    </div></body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been many threads started over the confusion in the way that Math.Round
After reading an interesting article about unit testing behavior instead of state, I came
I'm seeing some strange (to me anyway) behavior when using MakeRelativeUri on Mono (2.6.7).
I have an onKeyDown event which is only needed for handling the Up and
WPF's RTB is terribly slow when working with any realistic amount of text. And
I am working on an application which does sequentially write a large file (and
My friends and I are working on some basic Ruby exercises to get a
If I call FormsAuthentication.SignOut(); with a user logged in (with createPersistentCookie set to false
This question is all over SO, but no one seems to have had the
I'm looking for some style advice for testing this piece of (Objective-)C++ code, I

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.