Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mozilla observer onclick

var observeDOM = (function(){
  var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

  return function( obj, callback ){
    if( !obj || obj.nodeType !== 1 ) return; 

    if( MutationObserver ){
      // define a new observer
      var mutationObserver = new MutationObserver(callback)

      // have the observer observe foo for changes in children
      mutationObserver.observe( obj, { childList:true, subtree:true })
      return mutationObserver
    }
    
    // browser support fallback
    else if( window.addEventListener ){
      obj.addEventListener('DOMNodeInserted', callback, false)
      obj.addEventListener('DOMNodeRemoved', callback, false)
    }
  }
})()


//------------< DEMO BELOW >----------------

// add item
var itemHTML = "<li><button>list item (click to delete)</button></li>",
    listElm = document.querySelector('ol');

document.querySelector('body > button').onclick = function(e){
  listElm.insertAdjacentHTML("beforeend", itemHTML);
}

// delete item
listElm.onclick = function(e){
  if( e.target.nodeName == "BUTTON" )
    e.target.parentNode.parentNode.removeChild(e.target.parentNode);
}
    
// Observe a specific DOM element:
observeDOM( listElm, function(m){ 
   var addedNodes = [], removedNodes = [];

   m.forEach(record => record.addedNodes.length & addedNodes.push(...record.addedNodes))
   
   m.forEach(record => record.removedNodes.length & removedNodes.push(...record.removedNodes))

  console.clear();
  console.log('Added:', addedNodes, 'Removed:', removedNodes);
});


// Insert 3 DOM nodes at once after 3 seconds
setTimeout(function(){
   listElm.removeChild(listElm.lastElementChild);
   listElm.insertAdjacentHTML("beforeend", Array(4).join(itemHTML));
}, 3000);
Comment

PREVIOUS NEXT
Code Example
Javascript :: about react frame 
Javascript :: compile pdf with javascript 
Javascript :: js get word before question mark 
Javascript :: js find selected input 
Javascript :: return where an property eqauls 
Javascript :: parallel and sequential implementation with asyn await javascript 
Javascript :: jquery select text with event target 
Javascript :: find short number in array javascript 
Javascript :: react store props in state 
Javascript :: runjs 
Javascript :: VueJs System Modifier keys like exact ctrl alt shift meta 
Javascript :: A react component can only return] 
Javascript :: jquery.MultiFile 
Javascript :: what does bang at the end of a statement mean for in typescript 
Javascript :: jquery 1.6 jgrid pagging ejemplo 
Javascript :: scale sprite matter.js 
Javascript :: How to set variable data in JSON body for the code that generated by Postman in c# 
Javascript :: convert javascript function to typescript online 
Javascript :: nested array generator for js 
Javascript :: js hex to string 
Javascript :: compass in react js 
Javascript :: eleventy stringify 
Javascript :: Get cheapest price phone from an object in javascript 
Javascript :: flyweight 
Javascript :: react date component with word month 
Javascript :: on inline style change 
Javascript :: grid square random number 
Javascript :: jquery hide elevateZoom 
Javascript :: cannot Nesting classes sass nextjs 
Javascript :: javascript array negative index 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =