DEV Community

Cover image for Discover the Power of JavaScript MutationObserver!πŸ’ͺπŸš€
Dharmendra Kumar
Dharmendra Kumar

Posted on

Discover the Power of JavaScript MutationObserver!πŸ’ͺπŸš€

A Comprehensive Guide to Monitoring DOM Changes

The MutationObserver API in JavaScript offers a powerful way to observe and react to changes in the DOM tree. Whether you're watching for updates to child elements, attributes, or even the text content within nodes, MutationObserver can help you handle these changes efficiently.


Introduction to MutationObserver

What is MutationObserver?

The MutationObserver API allows you to monitor changes in the DOM tree. Whenever there's a modification, a specified callback function gets executed, enabling you to react dynamically to these changes.

How to Use MutationObserver

  1. Define a Callback Function:
   function callback(mutations) {
       mutations.forEach(mutation => {
           console.log(mutation);
       });
   }
Enter fullscreen mode Exit fullscreen mode
  1. Create a MutationObserver Object:
   let observer = new MutationObserver(callback);
Enter fullscreen mode Exit fullscreen mode
  1. Start Observing DOM Changes:
   observer.observe(targetNode, { childList: true, attributes: true });
Enter fullscreen mode Exit fullscreen mode
  1. Stop Observing DOM Changes:
   observer.disconnect();
Enter fullscreen mode Exit fullscreen mode

Constructor: Creating a MutationObserver

MutationObserver()

To create a new MutationObserver instance, pass your callback function to the MutationObserver constructor:

let observer = new MutationObserver(callback);
Enter fullscreen mode Exit fullscreen mode

Example:

function logChanges(mutations) {
    mutations.forEach(mutation => {
        console.log(mutation);
    });
}

let observer = new MutationObserver(logChanges);
Enter fullscreen mode Exit fullscreen mode

Instance Methods: Controlling Your Observer

disconnect()

The disconnect() method stops the MutationObserver from receiving notifications of DOM changes. This can be useful when you no longer need to monitor the DOM or to improve performance.

Example:

observer.disconnect();
Enter fullscreen mode Exit fullscreen mode

observe()

The observe() method starts the MutationObserver instance, allowing it to monitor a specified DOM node and its subtree.

Example:

observer.observe(targetNode, {
    childList: true,
    attributes: true,
    subtree: true,
});
Enter fullscreen mode Exit fullscreen mode

takeRecords()

The takeRecords() method returns an array of all the mutation records that have been detected but not yet processed by the callback.

Example:

let records = observer.takeRecords();
records.forEach(record => {
    console.log(record);
});
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Observing Child Element Changes

HTML:

<ul id="language">
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<button id="btnAdd">Add</button>
<button id="btnRemove">Remove</button>
Enter fullscreen mode Exit fullscreen mode

JavaScript:

let list = document.querySelector('#language');
let btnAdd = document.querySelector('#btnAdd');
let btnRemove = document.querySelector('#btnRemove');

function log(mutations) {
    mutations.forEach(mutation => {
        if (mutation.type === 'childList') {
            console.log(mutation);
        }
    });
}

let observer = new MutationObserver(log);

observer.observe(list, { childList: true });

btnAdd.addEventListener('click', () => {
    let item = document.createElement('li');
    item.textContent = 'New Item';
    list.appendChild(item);
    console.log('Added new item');
});

btnRemove.addEventListener('click', () => {
    if (list.lastElementChild) {
        list.removeChild(list.lastElementChild);
        console.log('Removed last item');
    }
});
Enter fullscreen mode Exit fullscreen mode

Observing Child Element Changes

Observing Attribute Changes

JavaScript:

let div = document.querySelector('#myDiv');

function logAttributes(mutations) {
    mutations.forEach(mutation => {
        if (mutation.type === 'attributes') {
            console.log(mutation);
        }
    });
}

let observer = new MutationObserver(logAttributes);

observer.observe(div, { attributes: true });

div.setAttribute('data-test', 'newValue');
console.log('Updated data-test attribute');
Enter fullscreen mode Exit fullscreen mode

Observing Subtree Changes

JavaScript:

let parent = document.querySelector('#parent');

function logSubtreeChanges(mutations) {
    mutations.forEach(mutation => {
        if (mutation.type === 'childList' && mutation.addedNodes.length) {
            console.log(mutation);
        }
    });
}

let observer = new MutationObserver(logSubtreeChanges);

observer.observe(parent, { childList: true, subtree: true });

let child = document.createElement('div');
child.textContent = 'I am a new child!';
parent.appendChild(child);
console.log('Added a new child to the subtree');
Enter fullscreen mode Exit fullscreen mode

Observing Character Data Changes

JavaScript:

let textNode = document.querySelector('#textNode');

function logCharacterDataChanges(mutations) {
    mutations.forEach(mutation => {
        if (mutation.type === 'characterData') {
            console.log(mutation);
        }
    });
}

let observer = new MutationObserver(logCharacterDataChanges);

observer.observe(textNode, { characterData: true });

textNode.textContent = 'New Text Content';
console.log('Updated text content');
Enter fullscreen mode Exit fullscreen mode

Accessing Old Values

JavaScript:

let div = document.querySelector('#myDiv');

function logOldValues(mutations) {
    mutations.forEach(mutation => {
        if (mutation.type === 'attributes') {
            console.log(`Old value: ${mutation.oldValue}`);
        }
    });
}

let observer = new MutationObserver(logOldValues);

observer.observe(div, { attributes: true, attributeOldValue: true });

div.setAttribute('class', 'newClass');
console.log('Changed class attribute');
Enter fullscreen mode Exit fullscreen mode

Conclusion

MutationObserver is a versatile and powerful tool for monitoring and responding to changes in the DOM. By understanding its constructor and methods, you can efficiently manage dynamic content and ensure your web applications are responsive to real-time updates.

Happy Coding!

Top comments (0)