'MutationObserver for HTML template

Seems like MutationObserver is not working on a <template> tag. https://jsfiddle.net/brhya5uj/

What's the best way to get notified when a <template> changes?



Solution 1:[1]

Per the specification template element doesn't have any children. Instead, the contents is placed inside content property, which you should observe:

new MutationObserver(console.log)
  .observe(document.querySelector('template').content, {childList: true, subtree: true});

More info: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wOxxOm