Last active
May 28, 2024 18:34
-
-
Save BroFox86/686688c7b1195a5813c5dd278f8108d9 to your computer and use it in GitHub Desktop.
[Mutation observer] Constructor for instantiating new DOM mutation observers #jsTrick
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
/* ========================================================================== | |
Constructor for instantiating new DOM mutation observers. | |
More info here: https://developer.mozilla.org/ru/docs/Web/API/MutationObserver | |
Objects list: https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord | |
========================================================================== */ | |
// Select the node that will be observed for mutations | |
var target = document.getElementById('some-id'); | |
// Create the MutationObserver | |
var observer = new MutationObserver(function( mutations ) { | |
mutations.forEach(function( mutation ) { | |
console.log( "The " + mutation.attributeName + " attribute was modified." ); | |
}); | |
}); | |
// Options for the observer (which mutations to observe) | |
var config = { attributes: true, childList: true, characterData: true }; | |
// Start observing the target node for configured mutations | |
observer.observe( target, config ); | |
// Later, you can stop observing | |
observer.disconnect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment