Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda