javascriptdomdom-eventsdom-node

When is "DOMNodeInserted" event called?


DOMNodeInserted event is called when the node "be appended to", or "be appended"?

I ask this because the following code:

function AddTextToContainer () {
    var textNode = document.createTextNode ("My text");
    var container = document.getElementById ("container");

    if (container.addEventListener) {
        container.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
    }
    container.appendChild (textNode);
}

and that:

function AddTextToContainer () {
   var textNode = document.createTextNode ("My text");
   var container = document.getElementById ("container");

   if (textNode.addEventListener) {
       textNode.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
   }
   container.appendChild (textNode);
}

Both invoke OnNodeInserted in Chrome. Is it a bug?


Solution

  • This is from W3C

    DOMNodeInserted
    Fired when a node has been added as a child of another node. 
    This event is dispatched after the insertion has taken place. 
    The target of this event is the node being inserted.
    Bubbles: Yes
    Cancelable: No
    Context Info: relatedNode holds the parent node
    

    The key is the Bubbles: Yes - thats why its being fired on the container as well.