JavaScript MutationObserver function

The ability to track DOM tree modifications is provided via the MutationObserver interface. It is intended to take the place of the previous Mutation Events functionality, which was included in the DOM3 Events definition.

The DOM can often change in complex web projects. Because of this, there are times when your app may need to react to a particular modification to the DOM. Listening to DOM changes is possible via the MutationObserver interface.

JavaScript MutationObserver Syntax

  • The use of MutationObserver is straightforward. A callback function should be used to create an observer as the initial stage, like in:
  • Adding it to a DOM node is the next step, which is as follows:

Config is an object with parameters defining the changes to respond to and is returned by the function.

  • childList: alterations to the node's immediate children.
  • subtree: every descendent of a node.
  • attributes: characteristics of the node.
  • attributeFilter: A list of attribute names that allows you to see the ones you want to see.
  • characterData: for node observation.

The callback follows any modifications. The observer becomes the second object, and the modifications are passed on to the first argument as a list of MutationRecord objects.

How to operate JavaScript MutationObserver

To use the MutationObserver API, you must first:

  • The callback method that will be called when the DOM changes must first be defined:
  • Next, make a MutationObserver object and send the callback to the function of the object using the MutationObserver() method:

let observer = new MutationObserver(callback);

  • Third, start tracking the DOM changes by calling the observe() method.

observer.observe(targetNode, observerOptions);

There are two parameters in the observe() function. The target is the root of the node subtree that needs to be watched for changes. What DOM changes should be communicated to the observer's callback are specified by the parameters in the observerOptions parameter.

  • Call the disconnect() method to end watching the DOM updates.

observer.disconnect();

Examples

The following examples show the javascript mutationobserver api with a different type of data.

Example1

The following example shows the basic javascript mutationobserver api with simple information.

Output

The following image shows basic javascript mutationobserver output.

Output 1

Insert the characters in the input field

JavaScript MutationObserver function

Output 2

After inserting the characters

JavaScript MutationObserver function

Example 2

The following example shows the basic javascript mutationobserver api with a false option.

Output

The following image shows basic JavaScript mutationobserver output.

JavaScript MutationObserver function

JavaScript MutationObserver options

You can define options to characterize the MutationObserver by passing them as the second argument to the observe() method:

Not every choice has to be chosen. However, at minimum, one childList, attributes, or characterData must be set to true for the MutationObserver to function; otherwise, the observer() method will return an error.

The following properties are included in the MutationRecord object:

  • type: The mutation's type. It is either "characterData," "attributes," or "childList."
  • target: where the change takes place.
  • addedNodes/removedNodes: the newly or recently added nodes.
  • The added or removed nodes' previousSibling or nextSibling, respectively.
  • attributeName/attributeNamespace: the new name or namespace for the attribute.
  • oldValue: The previous value is only for text or attribute changes if attributeOldValue/characterDataOldValue is set as the matching option.

Example

The following example shows the basic javascript mutationobserver api with multiple options.

Output

The following image shows basic JavaScript mutationobserver output.

JavaScript MutationObserver function

Observe the changes in a child element

The mutation method can show the child element start, stop, add, and remove functionality. The following steps help to show changes in the child element of the function.

  • Here, we can create a list of the data in html.
  • Create buttons for the functionality in the html.
  • Create a variable of the buttons and data tag using the query selector of javascript.

Use the mutation function and callback with the variable

  • Add required operation with mutationobserver variable and JavaScript function

Examples

The following examples show the child list and its data with different operations.

Example 1

The example shows the basic changes in the child element of the parent element.

Output

The image shows the output of the javascript mutationobserver with the child element.

Output1

Click start button

JavaScript MutationObserver function

Output2

Click stop button

JavaScript MutationObserver function

Example 2

The example shows the insert data in the child element of the parent element.

Output

The given image shows the output of the javascript mutationobserver with the child element.

Output1

Before inserting the last element

JavaScript MutationObserver function

Output2

After inserting the last element

JavaScript MutationObserver function

Example 3

The example shows the deleted data in the child element of the parent element.

Output

The given image shows the output of the deleted last value of the list.

Output1

Before removing the last element

JavaScript MutationObserver function

Output2

After removing the last element

JavaScript MutationObserver function

Keep in mind the mutationObserver crucial details.

  • The callback function, which I've given the name mCallback to demonstrate that you may give it any name, will activate every time a valid mutation is discovered and following the conclusion of the watch() method.
  • Since childList is the only "kind" of acceptable mutation, it makes sense to search for it when looping through the MutationRecord. In this case, searching for any other type would be useless.
  • A text node can be added or removed from the targeted element using childList, which would also count. This means that it is not necessary to add or remove an element.

Conclusion

The javascript mutationobserver method observes and operates the list. Here, we can operate and work on the list and its child data with multiple options.






Latest Courses