Thursday, June 20, 2013

Detecting When DOM Elements Have Been Removed With jQuery

If you look at the W3C, there is actually an event that gets triggered when a DOM element is removed from a document sub-tree:
DOMNodeRemoved: Fires when a node has been removed from a DOM-tree.
This DOM event will bubble up the document tree with the removed node as its target. But of course, even though this is a standard in the W3C, it's not fully supported in the various browsers. And, somewhat to be expected, from my brief testing, the one browser that I have that doesn't support this event type is Internet Explorer. However, if we are going to be using jQuery to perform our DOM mutations, we can actually simulate this event if the current browser is IE.


function common__NodeRemovedEventTrigger() {
    jQuery( "body" ).bind(
        "DOMNodeRemoved",
        function( objEvent ){
            // Append event to log display.
            var elm = jQuery(objEvent.target);
            if(elm.attr("class") !== undefined) {
                console.log("Removed element class name: "+elm.attr("class"));
            }
        }
    );
}

http://www.bennadel.com/blog/1623-Ask-Ben-Detecting-When-DOM-Elements-Have-Been-Removed-With-jQuery.htm 

No comments:

Post a Comment