Basic Usage
Basic Usage
Section titled “Basic Usage”Introduction
Section titled “Introduction”Create a Mutation Observer with a callback, configure which changes to observe, and start watching a target element.
Basic Setup
Section titled “Basic Setup”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 observingobserver.disconnect();Configuration Options
Section titled “Configuration Options”| Option | Default | Watches For |
|---|---|---|
childList | false | Direct child additions/removals |
attributes | false | Attribute changes |
subtree | false | All descendants (not just direct) |
characterData | false | Text content changes |
Summary
Section titled “Summary”- Create with
new MutationObserver(callback) - Callback receives array of MutationRecord objects
- Configure what to observe with
configobject observer.disconnect()stops all observation