MutationObserver to check an item appear or disappear from DOM

Viewed 146

I've below code to observe a modal, but it doesn't seem to work

var observer = new MutationObserver(function (mutations) {
   mutations.forEach(function (mutation) {
      if(mutation) { console.log('appeared') }
   })
})


var node = document.querySelector('[class^="selection-menu-container"]')
observer.observe(node, {childList: true, substree: true})

Any clue what's missing? When I do console.log(node) I can see it is targeting the modal. Note that the selection-menu-container class isn't available on the DOM on first load, can that be the cause of the issue?

1 Answers

MutationObserver only work with one level of direct children, it won't work for more than 1 level of nested children.

Related