Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript mutation observer

var fnCallback = function (mutations) {
    mutations.forEach(function (mutation) {
        alert(mutation.target.id, "added: " + mutation.addedNodes.length + " nodes");
        alert(mutation.target, "removed: " + mutation.removedNodes.length + " nodes");
    });
};

var observer = new MutationObserver(fnCallback),
        elTarget = document.querySelector("#divTarget"),
        objConfig = {
            childList: true,
            subtree : true,
            attributes: true, 
            characterData : true,
            attributeFilter : ['style', 'id'],
            attributeOldValue : true
        };

    observer.observe(elTarget, objConfig);
    
    
    
    
    
    
    
//below here is just the test code    
var btnUpdate = document.querySelector("#btnUpdate"),
    fnHandler = function () {
        elTarget.innerHTML = "<h2>New content</h2>";
    };
    
    btnUpdate.addEventListener("click", fnHandler, false);
Source by jsfiddle.net #
 
PREVIOUS NEXT
Tagged: #javascript #mutation #observer
ADD COMMENT
Topic
Name
6+3 =