Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mutation observer js

const observer = new MutationObserver(list => {
    console.log('mutation list', list);
});
observer.observe(document.body, {
    attributes: true,
    childList: true,
    subtree: true
});
// perform any DOM change action in your page. e.g. show/hide/remove
Comment

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);
Comment

mutation observer

  var composeObserver = new MutationObserver(function(mutations){ 
    mutations.forEach(function(mutation){
      console.log("MUTATE:", mutation)
    });
});


  function addObserverIfDesiredNodeAvailable() {
    console.log('calling')
    var composeBox = document.querySelector(".title");
    console.log("compose box: ", composeBox);
    if(!composeBox) {
        //The node we need does not exist yet.
        //Wait 500ms and try again
        window.setTimeout(addObserverIfDesiredNodeAvailable,500);
        return;
    }
    var config = {childList: true};
    composeObserver.observe(composeBox,config);
}
addObserverIfDesiredNodeAvailable();
Comment

How to write a mutation observer js

let mList = document.getElementById('myList'),
            options = {
                childList: true,
                attributes: true,
                subtree: true
            },
            observer = new MutationObserver(mCallback);

        function mCallback(mutations) {
            for (let mutation of mutations) {
                // If you remove a child from the container you are watching
                if (mutation.type === 'childList') {
                    console.log('Mutation Detected: A child node has been added or removed.');
                }
                // If you use setAttribute to add a class or ID to an element
                if (mutation.type === 'attributes') {
                    console.log('Mutation Detected: An attribute has been added or removed.');
                }

                if (mutation.type === 'subtree') {
                    console.log('Mutation Detected: A subtree has been added or removed.');
                }
            }
        }

observer.observe(mList, options);
Comment

PREVIOUS NEXT
Code Example
Javascript :: JavaScript Initialize Variables 
Javascript :: how to upload file with button react 
Javascript :: iterating string js 
Javascript :: copy on clip board 
Javascript :: find intersection between two object arrays javascript 
Javascript :: scroll up btn 
Javascript :: leaflet marker 
Javascript :: binary to decimal javascript 
Javascript :: input show validation message 
Javascript :: document.getanimation 
Javascript :: angularjs make post request 
Javascript :: max value javascript 
Javascript :: Javascript using for loop to loop through an array 
Javascript :: jquery if else click function 
Javascript :: find object and remove in array 
Javascript :: How to Use the replace() String Method in javascript 
Javascript :: are you sure you want to close this window javascript 
Javascript :: double click on element using javascript 
Javascript :: how to aadd variable in html tag in js 
Javascript :: Math max with array js 
Javascript :: angular ngstyle variable 
Javascript :: xml vs json 
Javascript :: Sorting Data Accessor 
Javascript :: math floor html 
Javascript :: console.table in javascript 
Javascript :: remove equal json js 
Javascript :: expiry data of jwt token 
Javascript :: JQuery Autocomplete no result found 
Javascript :: Conditional expressions and in fandom explained examples 
Javascript :: sequelize order by nulls last 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =