Skip to content

Instantly share code, notes, and snippets.

@masterfermin02
Created April 3, 2018 20:23
Show Gist options
  • Save masterfermin02/5536b3b5e551840bee446e5cfddffa2f to your computer and use it in GitHub Desktop.
Save masterfermin02/5536b3b5e551840bee446e5cfddffa2f to your computer and use it in GitHub Desktop.
observeDOM
var observeDOM = (function(){
var MutationObserver = window.MutationObserver ||
window.WebKitMutationObserver;
return function(obj, thistime, callback){
if(typeof obj === 'undefined'){
console.log('obj is undefined');
return;
}
if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length ){
callback('pass other observations back...');
}else if(mutations[0].attributeName == "value" ){
// use callback to pass back value of hidden form field
callback( obj.value );
}
});
// have the observer observe obj for changes in children
// note 'attributes:true' else we can't read the input attribute value
obs.observe( obj, { childList:true, subtree:true, attributes:true });
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment