Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!