Search code examples
javascriptconsole.logmutation-observers

Using MutationObserver to log changes


I want to be able to console.log out the price each time the price changes. I'm trying to use MutationObserver to do this which does seem to work when the price is changed, that a price is logged out. But for some reason I keep logging out the same price i.e the cheapest size. Any ideas where I'm going wrong?

const elementToObserve = document.querySelector(".productItem");
const size = document.querySelector('.itemSize');
const observer = new MutationObserver(function() {
  console.log(size.innerHTML);
});

observer.observe(elementToObserve, {
  subtree: true,
  childList: true
});


Solution

  • Instead of using .innerHTML to get the inner content, try using .textContent

    console.log(size.textContent);