Categories
JavaScript Answers

How to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript?

Spread the love

Sometimes, we want to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript.

In this article, we’ll look at how to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript.

How to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript?

To fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript, we should make sure the element we’re observing is available.

For instance, we write

const composeObserver = new MutationObserver(function (mutations) {
  mutations.forEach((mutation) => {
    mutation.addedNodes.forEach((node) => {
      //...
    });
  });
});

const composeBox = document.querySelector(".no");
if (composeBox) {
  const config = { childList: true };
  composeObserver.observe(composeBox, config);
}

to select the element to observer with querySelector.

Then if it’s not null, we call observe to observer the changes in the element.

Conclusion

To fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript, we should make sure the element we’re observing is available.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *