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

OptionTypeDescription
childListbooleanWatch for child node additions/removals
attributesbooleanWatch for attribute changes
characterDatabooleanWatch for text content changes
subtreebooleanExtend observation to descendants
attributeOldValuebooleanRecord previous attribute values
characterDataOldValuebooleanRecord previous text content
attributeFilterstring[]List of attributes to observe

MutationRecord Properties

PropertyDescription
type'attributes', 'characterData', or 'childList'
targetThe node affected by the mutation
addedNodesNodeList of added nodes
removedNodesNodeList of removed nodes
previousSiblingPrevious sibling of added/removed nodes
nextSiblingNext sibling of added/removed nodes
attributeNameName of changed attribute
oldValuePrevious 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
.2 Mutation Observer - JavaScript Tutorial | DeepML