Docs
19.2-Mutation-Observer
19.2 Mutation Observer
Overview
MutationObserver provides a way to watch for changes being made to the DOM tree, replacing the deprecated Mutation Events.
MutationObserver Flow
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā MutationObserver Flow ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā 1. Create Observer ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā new MutationObserver(cb) ā ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā ā
ā ā¼ ā
ā 2. Configure & Observe ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā observer.observe(target, { ā ā
ā ā childList: true, ā ā
ā ā attributes: true, ā ā
ā ā subtree: true ā ā
ā ā }) ā ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā ā
ā ā¼ ā
ā 3. Mutations Happen ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā DOM changes are collected ā ā
ā ā in a batch (microtask) ā ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā ā
ā ā¼ ā
ā 4. Callback Invoked ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā callback(mutationsList, obs)ā ā
ā ā Process all mutations ā ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Configuration Options
| Option | Type | Description |
|---|---|---|
childList | boolean | Watch for child node additions/removals |
attributes | boolean | Watch for attribute changes |
characterData | boolean | Watch for text content changes |
subtree | boolean | Extend observation to descendants |
attributeOldValue | boolean | Record previous attribute values |
characterDataOldValue | boolean | Record previous text content |
attributeFilter | string[] | List of attributes to observe |
MutationRecord Properties
| Property | Description |
|---|---|
type | 'attributes', 'characterData', or 'childList' |
target | The node affected by the mutation |
addedNodes | NodeList of added nodes |
removedNodes | NodeList of removed nodes |
previousSibling | Previous sibling of added/removed nodes |
nextSibling | Next sibling of added/removed nodes |
attributeName | Name of changed attribute |
oldValue | Previous value (if requested) |
Basic Usage
// Create observer
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
console.log('Children changed');
} else if (mutation.type === 'attributes') {
console.log(`${mutation.attributeName} changed`);
}
}
});
// Start observing
observer.observe(element, {
childList: true,
attributes: true,
subtree: true,
});
// Stop observing
observer.disconnect();
// Get pending mutations
const pending = observer.takeRecords();
Common Use Cases
- ā¢Dynamic Content: React to third-party script changes
- ā¢Form Validation: Watch input value changes
- ā¢Analytics: Track UI interactions
- ā¢Polyfills: Implement custom element behavior
Summary
- ā¢MutationObserver is async and batched
- ā¢Configure precisely what to observe
- ā¢Use subtree for deep observation
- ā¢Call disconnect() when done
- ā¢Avoid infinite loops in callbacks