![]() |
VOOZH | about |
Mutation Observer is a web API (web APIs are APIs provided by the browser itself) that is used to observe (i.e.; detect) the changes of the target HTML element within a web page. For example, when the user wants to know which element has recently been removed and which element has been added to the DOM, we can use Mutation Observer. In dynamic websites, we know that elements are added, removed, or modified dynamically, this API serves as a tool for tracking and reacting to these changes. In this article, we will see the Mutation Observer in the Web API, along with understanding its benefits, use cases, instance methods, & etc.
// Create observer API instance
observer=new MutationObserver( intersectionCallback, config );
// Define intersectionCallback() function to
// do operations when mutation happens.
function intersectionCallback( entries, observer )
// Call observe() method to observe the target HTML element.
observer.observe( targetElement );
Now we know why mutation observer is a more convenient way of observing mutations in the DOM elements, let's know about its use cases before diving deeper into real-world examples of detecting a DOM mutation.
Some possible approaches to use Mutation Observer API are:
We have a single <div> element (our target ). Initially, the color of this <div> is red, then we are going to change its color from red to blue and then blue to green in the intervals of 4 sec and 6 sec. So our objective is to detect the moment when mutation of colors happens using mutation observer API.
Example: First we will change the color of the target within the specified interval using the SetTimeout() function. Now to detect the change, we will use attributes and attributeFilter property of the config option object because we are only changing the color property of the style of the target element.
Explanation: Initially the color was red. After 4 sec, it changed from red to blue, and then after 2 more seconds, changed to green. MutationObserver API is fully able to detect the changes successfully and print color values onto the console.
Output:
We are given an editable paragraph text. When we add a new line to the text paragraph and hit ENTER, then our program should detect this mutation of adding a new line in the given paragraph and should log the changed paragraph on the console. Detection of the first line of a given paragraph is not required.
Example: we will detect only the changes made directly to the target element (i.e.; paragraph ). If the change is in childList==true, then check when the user is going to hit ENTER (i.e.; line break ). If a line break is found, it means the user has completed adding the data into the current line and entered into a new line. so now we can update our current paragraph data and log the same on the console.
Output:
Javascript provides some other observer APIs like Intersection Observer, Resize Observer, etc. Each observer API has different use cases and is introduced to provide some kind of optimizations over the traditional way of observing DOM elements.