Skip to content

Instantly share code, notes, and snippets.

@BroFox86
Last active May 28, 2024 18:34
Show Gist options
  • Save BroFox86/686688c7b1195a5813c5dd278f8108d9 to your computer and use it in GitHub Desktop.
Save BroFox86/686688c7b1195a5813c5dd278f8108d9 to your computer and use it in GitHub Desktop.
[Mutation observer] Constructor for instantiating new DOM mutation observers #jsTrick
/* ==========================================================================
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