-1

I did some work and couldn't make it to the working conditions.

Posting here to get some help.

In my HTML view, there is a class.

So to get some results I'm using AJAX code and in return according to the result of the JSON value, I want to change the class to another.

This is the code

  <div class="stats-small__data">
    <span class="stats-small__percentage" id="SatisPrecent"></span>
  </div>

so this span class mainly has,

  1. stats-small__percentage stats-small__percentage--decrease
  2. stats-small__percentage stats-small__percentage--increase

I want to change with the result of ,

 if (data.SatisAva > 50) {
   document.getElementById("SatisPrecent").classList.add = 'stats-small__percentage--decrease';
 } else {

   document.getElementById("SatisPrecent").classList.add = 'stats-small__percentage--increase';
 }

So the current code is not working as I wanted to. Little help here?

  • `classList.add` is a normal method, not a setter – CertainPerformance May 07 '22 at 04:14
  • @CertainPerformance how to set it ? Im still a beginner to this. – Dev Beginner May 07 '22 at 04:19
  • From your example, you are trying to append the class string instead of adding a new class. You can do `document.getElementById("SatisPrecent").className += '--decrease'` for example. But it is not a very good way of dealing with classes. You should probaly use `document.getElementById("SatisPrecent").classList.add('stats-small__percentage--decrease')` or increase based on the criteria. – Han Moe Htet May 07 '22 at 04:43

0 Answers0