I have a simple installation of MutationObserver
as a test. HTML has a span
whose text content is updated once per second (and a div
for messages):
<span class="tester"></span> <div id="msg"></div>
MutationObserver is configured to view .tester
and write text in the #msg
div
when it notices a change. Meanwhile, setInterval()
works once / second to change the text in .tester
:
var target = document.querySelector('.tester'); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { console.log(mutation.type); $('#msg').append(mutation.type+"<br/>") setTimeout(function() { $('#msg').text(''); }, 500); }); }); var config = { attributes: true, childList: true, characterData: true }; observer.observe(target, config); setInterval(function() { $('#msg').text(''); $('.tester').text("update: "+Math.random()) }, 1000);
I would expect this code to print once per second that characterData has changed. According to Mozilla's docs for MutationObserver , he talks about characterData: "Set to true if you need to track mutations for the target data." Instead, I don't see characterData mutations, but each time I see two childList mutations.
Why don't I see any characterData mutations and why do I see two childList mutations?
Here is a working example with CodePen .
source share