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

212
Views
Why can't I change a variable's textContent's atribute in an if-statement?

Why doesn't variable span_perc's coloring change to red even though I know I've just updated it's textContent to '-28.86%'?

If I log span_perc.textContent to console it says that it's a even though that's not true because it can change it to be green and I can see it on the website.


    <div class="col-12 xl:col-6">
        <div class="card">
            <h5>Total Profits</h5>
            <span id="profits_perc"></span>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <span id="profits_money"></span>
            <Chart type="line" :data="lineData" />
        </div>
    </div>
    </div>
</template>

<script>
import ProductService from '../service/ProductService';
import axios from 'axios';

window.onload = dostuff;

function dostuff() {
    var span_perc = document.getElementById("profits_perc");
    var span_money = document.getElementById("profits_money");
    axios.get('http://localhost:8000/').then(response => {span_perc.textContent = response.data[2].profits_perc});
    axios.get('http://localhost:8000/').then(response => {span_money.textContent = response.data[2].profits_cash});
    if (span_perc.textContent == '-28.86%') {
        span_perc.setAttribute("style", "color:red")
    } else {
        span_perc.setAttribute("style", "color:green")
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It is because the values are changed in an async way inside an XHR call, if you include your if condition inside the then block, it should work:

axios.get('http://localhost:8000/').then(response => {
span_perc.textContent = response.data[2].profits_perc;
if (span_perc.textContent == '-28.86%') {
        span_perc.setAttribute("style", "color:red")
    } else {
        span_perc.setAttribute("style", "color:green")
    }
});
about 4 years ago · Juan Pablo Isaza Report

0

Try performing the changes within the asynchronous call.

Why the nature of async work is that while we wait for the promise to resolve as some data or an error the rest of the code will continue to execute.

function dostuff() {
      var span_perc = document.getElementById("profits_perc");
      var span_money = document.getElementById("profits_money");
      axios.get("http://localhost:8000/").then((response) => {
        span_perc.textContent = response.data[2].profits_perc;
        if (span_perc.textContent == "-28.86%") {
            span_perc.setAttribute("style", "color:red");
          } else {
            span_perc.setAttribute("style", "color:green");
          }
      });
      axios.get("http://localhost:8000/").then((response) => {
        span_money.textContent = response.data[2].profits_cash;
      });
      
    }
about 4 years ago · Juan Pablo Isaza 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!