Skip to content

Basic Usage

Create a Mutation Observer with a callback, configure which changes to observe, and start watching a target element.

const targetNode = document.getElementById('observable');
const config = {
childList: true, // Watch for child element changes
attributes: true, // Watch for attribute changes
subtree: true, // Watch all descendants
characterData: true // Watch text content changes
};
const observer = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
if (mutation.type === 'childList') {
console.log('Nodes added:', mutation.addedNodes);
console.log('Nodes removed:', mutation.removedNodes);
}
if (mutation.type === 'attributes') {
console.log(`Attribute "${mutation.attributeName}" changed`);
}
});
});
observer.observe(targetNode, config);
// Stop observing
observer.disconnect();
OptionDefaultWatches For
childListfalseDirect child additions/removals
attributesfalseAttribute changes
subtreefalseAll descendants (not just direct)
characterDatafalseText content changes
  • Create with new MutationObserver(callback)
  • Callback receives array of MutationRecord objects
  • Configure what to observe with config object
  • observer.disconnect() stops all observation