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

115
Visualizações
Javascript Variable Not Changing Value With Html Content Changing

I have a code that im trying to make a variable that changes if the price of the product changes hers the code:

<h3 class="single-price"><span class="after currency-value"><span class="value">5000</span></h3>

var price_aft = document.querySelector("#app > main > div > div > div.container > div > div.product-details > div.product-section.price-section > h3 > span.after.currency-value > span.value").textContent

when the price changes the variable value does not change? can someone help me please! thank you.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The HTML is malformed. So, I'm not sure you mean:

A:

<h3 class="single-price">
  <span class="after currency-value">
    <span class="value">5000</span>
  </span>
</h3>

or B:

<h3 class="single-price">
  <span class="after currency-value"></span>
  <span class="value">5000</span>
</h3>

So, I went with option A:

let selector = ".single-price .after .value";
let priceAft = document.querySelector(selector);
console.log(priceAft.innerHTML);
<h3 class="single-price">
  <span class="after currency-value">
    <span class="value">5000</span>
  </span>
</h3>

about 4 years ago · Santiago Trujillo Relatório

0

WOW, that's an enormous selector chain,

first of all, make sure your selector is correct, you can remove .textContent from the end of your js code and see if querySelector can find your element (use console.log to see what is stored in your variable)

then if the variable has undefined value, your selector is wrong if the element was selected correctly, try out innerText instead of textContent and let me know of the result

about 4 years ago · Santiago Trujillo Relatório

0

  // select span
  var target = document.getElementById('value');
var currValue = '';

// create observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log("event trigger " + mutation.target.id);
  });
});

// pass what to listen
var config = {
  attributes: true,
  childList: true,
  characterData: true
};

// pass span node and config
observer.observe(target, config);

// simulate the Change of the text value of span
function onValueChange() {
  currValue = target.textContent;
  console.log(currValue);
}

setTimeout(onValueChange, 2000);
<h3 class="single-price"><span class="after currency-value"><span id="value" class="value">5000</span></h3>

about 4 years ago · Santiago Trujillo 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