Mutation Observers---subtree

The documentation is unclear, but subtree is ignored unless you also specify childList:true.

The case for attributes and attributeFilter is the same.

Hope it still helps.


In mutationObserver config, at least one of attributes, characterData, or childList needs to be set true.

Now, If you just set childList: true, then it will observe only the direct children (depth 1) of the target element and not the complete subtree.

To observe the complete subtree both childList and subtree needs to be set true.

e.g.

{
   childList: true,
   subtree: true
}

I hope, it's helpful.


According to this article:

childList: Set to true if additions and removals of the target node's child elements (including text nodes) are to be observed.

subtree: Set to true if mutations to not just target, but also target's descendants are to be observed.

This also explains subtree depending on childList.