Browse by section

Web Design 日本語

Is MutationObserver Deprecated? No, Mutation Events Are

People search for “MutationObserver deprecated” and find mixed answers about whether it still works.

The short answer: MutationObserver is not deprecated. What was deprecated, and disabled by default in Chrome 127, is the similarly named Mutation Events API, such as DOMSubtreeModified. MutationObserver is the current API that replaces it.

This article explains the difference, what I saw when I tested both in Chrome 152, and a table for migrating from Mutation Events to MutationObserver.

Sponsored

Is MutationObserver deprecated?

No. Both Chrome’s announcement and MDN point to MutationObserver as the replacement for the deprecated Mutation Events.

The confusion comes from the names. Both start with “Mutation”, so “Mutation Events are deprecated” gets read as a statement about MutationObserver.

Item Mutation Events MutationObserver
How you use it addEventListener('DOMNodeInserted', ...) new MutationObserver(callback).observe(target, options)
Notification An event fires for each change Change records arrive together as an array
Status Deprecated, disabled by default in Chrome 127 Current standard, the recommended replacement

Chrome’s announcement, “Mutation events will be removed from Chrome”, tells developers to migrate code that uses Mutation Events to MutationObserver. It also notes that MutationObserver has been available in all modern browsers since 2013.

What are Mutation Events, and when were they removed?

Mutation Events are an old way to receive DOM changes as events, deprecated in the specification since 2011. Chrome disabled them by default starting with Chrome 127, which reached stable on July 23, 2024.

Chrome’s announcement lists these six events for removal:

▼Mutation Events disabled in Chrome

▶ DOMSubtreeModified

▶ DOMNodeInserted / DOMNodeRemoved

▶ DOMNodeInsertedIntoDocument / DOMNodeRemovedFromDocument

▶ DOMCharacterDataModified

To give sites time to migrate, Chrome offered a deprecation trial covering Chrome 124 to 134, and an enterprise policy named MutationEventsEnabled.

DOMAttrModified, which reports attribute changes, belongs to the same family. MDN’s MutationEvent page describes Mutation Events as pending removal from browsers and selected for removal as part of Interop 2025. It also notes that Safari never supported DOMAttrModified.

Do Mutation Events still work in current Chrome?

No. In Chrome 152.0.7977.83, DOMSubtreeModified, DOMNodeInserted and DOMAttrModified never fired, while MutationObserver received every change.

I attached both kinds of listeners to the same element, then changed its class once and added one child element.

const box = document.getElementById('box');
box.addEventListener('DOMSubtreeModified', () => log.push('DOMSubtreeModified fired'));
box.addEventListener('DOMNodeInserted', () => log.push('DOMNodeInserted fired'));
box.addEventListener('DOMAttrModified', () => log.push('DOMAttrModified fired'));

new MutationObserver((records) => {
  for (const r of records) log.push(`MutationObserver: type=${r.type}`);
}).observe(box, { attributes: true, attributeOldValue: true, childList: true, subtree: true });

box.className = 'b';
box.appendChild(document.createElement('span'));

This was the result:

typeof MutationEvent: undefined
MutationObserver: type=attributes attr=class old=a
MutationObserver: type=childList added=1

Note that there was no error and no warning. Registering the listeners did not throw, and the console stayed empty. Old code simply stops working without anyone noticing.

Sponsored

How do you migrate from Mutation Events to MutationObserver?

Every Mutation Event can be replaced by a combination of options passed to MutationObserver’s observe().

Mutation Events Options for observe() What to read in the callback
DOMNodeInserted / DOMNodeRemoved { childList: true } (add subtree: true for descendants) addedNodes / removedNodes
DOMAttrModified { attributes: true, attributeOldValue: true } (use attributeFilter for specific attributes) attributeName / oldValue
DOMCharacterDataModified { characterData: true, characterDataOldValue: true, subtree: true } oldValue / target.data
DOMSubtreeModified { childList: true, attributes: true, characterData: true, subtree: true } Branch on type
DOMNodeInsertedIntoDocument / DOMNodeRemovedFromDocument Observe document.body with { childList: true, subtree: true } Whether document.contains() finds the element

How do you watch for added and removed elements?

A single childList: true option replaces both DOMNodeInserted and DOMNodeRemoved.

const list = document.getElementById('list');
new MutationObserver((records) => {
  for (const r of records) {
    r.addedNodes.forEach((n) => console.log('added', n.nodeName, n.textContent));
    r.removedNodes.forEach((n) => console.log('removed', n.nodeName, n.textContent));
  }
}).observe(list, { childList: true });

After adding two li elements and removing one, Chrome 152 delivered “added LI 2”, “added LI 3” and “removed LI 2” in that order.

How do you watch for attribute changes?

If you know which attribute matters, attributeFilter keeps unrelated changes out.

const box = document.getElementById('box');
new MutationObserver((records) => {
  for (const r of records) {
    console.log(r.attributeName, r.oldValue, '→', box.getAttribute(r.attributeName));
  }
}).observe(box, { attributes: true, attributeOldValue: true, attributeFilter: ['class'] });

I changed the class from a to b and also changed data-x. Only one record arrived, class a → b. The data-x change was not recorded.

How do you watch for text changes?

Text node changes come through characterData. When you observe an element, you also need subtree: true.

const text = document.getElementById('text');
new MutationObserver((records) => {
  for (const r of records) console.log(r.oldValue, '→', r.target.data);
}).observe(text, { characterData: true, characterDataOldValue: true, subtree: true });

text.firstChild.data = 'world';

What changes is the text node inside the p element, not the p itself. Without subtree: true, observing the p gives you no notification.

How do you detect when an element is added to the page?

There is no option equivalent to DOMNodeInsertedIntoDocument. Observe additions across the page and check whether the element is now inside document.

const target = document.createElement('div');
const observer = new MutationObserver(() => {
  if (document.contains(target)) {
    console.log('added to the page');
    observer.disconnect();
  }
});
observer.observe(document.body, { childList: true, subtree: true });

const wrapper = document.createElement('section');
wrapper.appendChild(target);
document.body.appendChild(wrapper); // the target is added together with its parent

addedNodes contains only the parent section, not target itself. That is why the check uses document.contains() instead of searching addedNodes.

What behavior changes when you migrate?

The callback is not called at the moment of the change. The records arrive later, all together.

Mutation Events fired an event for each individual change. MutationObserver collects change records and hands them over after the current work finishes. I logged before and after the changes in Chrome 152 to confirm this:

before the changes
right after the changes (callback not called yet)
takeRecords() returned 3 pending records

After two additions and one removal, the callback had not run yet. To process records that have not been delivered yet, call takeRecords(). To stop observing, call disconnect().

Old code that expects an event handler to have run immediately after a change breaks because of this difference. When migrating, move the follow-up logic into the callback.

Which API should you use for class changes or visibility?

For class changes, use MutationObserver with attributeFilter: ['class']. To know when an element enters the viewport, use the Intersection Observer API.

I compared class-change detection with polling and Proxy in Detecting Class Changes in JavaScript With MutationObserver.

If you want to know whether an element is visible on screen, such as to trigger an animation, MutationObserver is the wrong tool. That is a job for the Intersection Observer API, which I covered in Scroll Animations with the Intersection Observer API.

Summary

▼Key points

① MutationObserver is not deprecated. The deprecated API is the similarly named Mutation Events

② Mutation Events were disabled by default in Chrome 127 (July 23, 2024), and MDN lists them as pending removal from other browsers too

③ In Chrome 152, Mutation Event listeners fail silently, with no error or warning

④ Migration is a matter of combining observe() options

⑤ Callbacks arrive later, in batches. Use takeRecords() when you need the records right away

Point 3 deserves the most attention. Leftover DOMSubtreeModified code does not throw, so nobody notices it stopped working. If a feature “just doesn’t work” anymore, searching the codebase for Mutation Event names is worth a try.

Tested with Chrome 152.0.7977.83 (macOS, headless mode) on September 12, 2026. Browser support changes over time, so check MDN for the latest status.