Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
I am trying to detect changes on a particular element using the MutationObserver but it is not working. What am I doing wrong?

I have been trying to log the updated cost information from an HTML document but have not been successful. I am sure it has to be that I am not using the mutation observer correctly. The js file is linked. Below is the HTML element that I am targeting.

HTML:

<span id="cost">0.00</span>

The JS file contains the following code:


const total = document.getElementById('cost');

console.log(total);
const options = {
  characterData: true
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    if (mutation.type === 'characterData')
    {
      console.log('Mutation Detected: Price changed');
    }
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total.childNodes[0], options);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code would work if the code that was making the change changed the text node you're watching rather than destroying and replacing it, like this:

total.childNodes[0].nodeValue = "1.00";

Live Example:

const total = document.getElementById('cost');

const options = {
  characterData: true,
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    if (mutation.type === 'characterData')
    {
      console.log('Mutation Detected: Price changed');
    }
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total.childNodes[0], options);

// Code that changes the element
total.childNodes[0].nodeValue = "1.00";
<span id="cost">0.00</span>

But the code making the change is probably destroying and replacing the child node, like this:

total.innerText = "1.00";

To handle that, you're probably best off watching for childList changes on the total element itself:

const total = document.getElementById('cost');

const options = {
  childList: true,
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    console.log('Mutation Detected: Price changed');
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total, options);

// Code that changes the element
total.innerText = "1.00";
<span id="cost">0.00</span>

You may need to refine that (I removed the if checking only for characterData changes), and/or combine it with what you already had (in case some other code changes the text differently), but that's the main issue with what you had.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda