Home > Software design >  MutationObserver for HTML template
MutationObserver for HTML template

Time:04-14

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?

CodePudding user response:

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

  • Related