Last active
December 5, 2020 01:02
-
-
Save smhigley/75fa44df8218d62714ea6df5bab353b5 to your computer and use it in GitHub Desktop.
MutationObserver quick test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function observeElement(element) { | |
const config = { attributes: true, childList: true, subtree: true }; | |
const callback = function(mutationsList, observer) { | |
for(let mutation of mutationsList) { | |
if (mutation.type === 'childList') { | |
const additions = mutation.addedNodes; | |
const removals = mutation.removedNodes; | |
if (removals.length) { | |
console.log('Child nodes were removed:', ...removals); | |
} | |
if (additions.length) { | |
console.log('Child nodes were added:', ...additions); | |
} | |
} | |
else if (mutation.type === 'attributes') { | |
console.log('The ' + mutation.attributeName + ' attribute was modified.'); | |
} | |
} | |
}; | |
const observer = new MutationObserver(callback); | |
observer.observe(element, config); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment