The MutationObserver interface provides the ability to watch for changes being made to the DOM tree. It is designed as a replacement for the older Mutation Events feature which was part of the DOM3 Events specification.
Constructor
MutationObserver()- Creates and returns a new
MutationObserverwhich will invoke a specified callback function when DOM changes occur.
Methods
disconnect()- Stops the
MutationObserverinstance from receiving further notifications until and unlessobserve()is called again. observe()- Configures the
MutationObserverto begin receiving notifications through its callback function when DOM changes matching the given options occur. takeRecords()- Removes all pending notifications from the
MutationObserver's notification queue and returns them in a newArrayofMutationRecordobjects.
Mutation Observer & customize resize event listener & demo
https://codepen.io/webgeeker/full/YjrZgg/
Example
The following example was adapted from this blog post.
// Select the node that will be observed for mutations
var targetNode = document.getElementById('some-id');
// Options for the observer (which mutations to observe)
var config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
var callback = function(mutationsList, observer) {
for(var mutation of mutationsList) {
if (mutation.type == 'childList') {
console.log('A child node has been added or removed.');
}
else if (mutation.type == 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
// Create an observer instance linked to the callback function
var observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
observer.disconnect();
See also
Specifications
| Specification | Status | Comment |
|---|---|---|
| DOM The definition of 'MutationObserver' in that specification. |
Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Chrome
Full support
26
| Edge Full support Yes | Firefox Full support 14 | IE Full support 11 | Opera Full support 15 | Safari
Full support
7
| WebView Android
Full support
Yes
| Chrome Android
Full support
26
| Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android Full support 14 | Safari iOS
Full support
7
| Samsung Internet Android Full support Yes |
MutationObserver() constructor | Chrome
Full support
26
| Edge Full support Yes | Firefox Full support 14 | IE Full support 11 | Opera Full support 15 | Safari
Full support
7
| WebView Android
Full support
Yes
| Chrome Android
Full support
26
| Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android Full support 14 | Safari iOS
Full support
7
| Samsung Internet Android Full support Yes |
observe | Chrome Full support 18 | Edge Full support Yes | Firefox Full support 14 | IE Full support 11 | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android Full support 14 | Safari iOS Full support 6 | Samsung Internet Android Full support Yes |
disconnect | Chrome Full support 18 | Edge Full support Yes | Firefox Full support 14 | IE Full support 11 | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android Full support 14 | Safari iOS Full support 6 | Samsung Internet Android Full support Yes |
takeRecords | Chrome Full support 18 | Edge Full support Yes | Firefox Full support 14 | IE Full support 11 | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android Full support 14 | Safari iOS Full support 6 | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- Requires a vendor prefix or different name for use.
- Requires a vendor prefix or different name for use.
Document Tags and Contributors
Tags:
Contributors to this page:
kfuchs,
xgqfrms,
Pamplemousse,
fscholz,
Sheppy,
KingSupernova,
kdex,
benjaminspak,
juliandescottes,
brendenseidel,
pingyen,
waldir,
Njol,
thewarpaint,
lsolova,
DomenicDenicola,
RinkAttendant6,
aknudsen,
libbymc,
RyanNerd,
layonthebeech,
jwhitlock,
raeesiqbal,
DevelX,
cvrebert,
chrisdavidmills,
KMStanton,
lydell,
Sebastianz,
ShyykoSerhiy,
jdanyow,
Lagg,
NiGhTCrAwLeR,
mattlunn,
mikehenrty,
jcsmnt0,
Jeremie,
Noitidart,
kscarfone,
jtAssi,
myakura,
pseudosavant,
teoli,
aklein,
rudiedirkx,
Rob W,
Victor_Homyakov,
ziyunfei,
Brettz9,
MDR,
aurimas,
kohei.yoshino,
codepanda,
kmaglione,
rpncreator,
Nickolay,
Canuckistani
Last updated by:
kfuchs,