Skip to content

Instantly share code, notes, and snippets.

@CompuWiser
Created April 10, 2021 05:55
Show Gist options
  • Save CompuWiser/f9dc6bce27f6d346621a5a9a01e9f410 to your computer and use it in GitHub Desktop.
Save CompuWiser/f9dc6bce27f6d346621a5a9a01e9f410 to your computer and use it in GitHub Desktop.
MutationObserver.md
// Select the node that will be observed for mutations
const targetNode = document.querySelector('#order_review');

// Options for the observer (which mutations to observe)
const config = {
  attributes: false,
  attributeFilter: ['class'],
  attributeOldValue: false,
  characterData: false,
  characterDataOldValue: false,
  childList: false,
  subtree: false
};

// Callback function to execute when mutations are observed
const callback = function (mutationsList, observer) {
  // Use traditional 'for loops' for IE 11
  for (const mutation of mutationsList) {
    console.log(mutation);
  }
};

// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);

// Start observing the target node for configured mutations
observer.observe(targetNode, 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