# How can i change color text when value less than 0

**URL:** <https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835>\
**Category:** General\
**Created:** [October 15, 2018, 3:38am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835 "2018-10-15T03:38:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DuongQuocViet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/duongquocviet/32/102893_2.png) [@DuongQuocViet](https://forum.freecodecamp.org/u/DuongQuocViet)\
**Post date:** [October 15, 2018, 3:38am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/1 "2018-10-15T03:38:59Z")

</div>

Hello! Everyone please help me ! my function not working!

```auto
<script>
    function addColor () { 

      var udata = document.getElementById("centerbox1").value;

      if (udata > 0) {
      document.getElementById("centerbox1").style.color = '#99C262';
      }
      else  
      if (udata < 0)  
      {
      document.getElementById("centerbox1").style.color = '#FF6C60';
      }  
      else  
      if (udata === 0)  
      {
      document.getElementById("centerbox1").style.color = '#1456a8';
      }
   };
</script>
<div class="containerbox">
  <div id="centerbox1">
      <div class="value">
          <p> <span>1</span>

              <br/><span>Utilisation</span>

          </p>
      </div>
  </div>
</div>                                       

```

---

<div class="post-metadata">

**Author:** ![shimphillip](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shimphillip/32/158663_2.png) [@shimphillip](https://forum.freecodecamp.org/u/shimphillip)\
**Post date:** [October 15, 2018, 3:51am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/2 "2018-10-15T03:51:50Z")

</div>

> [@DuongQuocViet](#):
>
> else if (udata \< 0) { document.getElementById(“centerbox1”).style.backgroundColor = ‘#FF6C60’; }

In this else if statement,  
you can do the same thing but change backgroundColor to color.

---

<div class="post-metadata">

**Author:** ![DuongQuocViet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/duongquocviet/32/102893_2.png) [@DuongQuocViet](https://forum.freecodecamp.org/u/DuongQuocViet)\
**Post date:** [October 15, 2018, 3:58am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/3 "2018-10-15T03:58:06Z")

</div>

thanks @shimphillip for the answer

---

<div class="post-metadata">

**Author:** ![JordanMarsh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jordanmarsh/32/102627_2.png) [@JordanMarsh](https://forum.freecodecamp.org/u/JordanMarsh)\
**Post date:** [October 15, 2018, 4:00am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/4 "2018-10-15T04:00:51Z")

</div>

```auto
else if (udata < 0) { document.getElementById(“centerbox1”).style.color = ‘#FF6C60’; }

```

or apply both:

```auto
let element = document.getElementById(“centerbox1”);
element.style.color = "somecolor";
element.style.backgroundColor = "somecolor";

```

---

<div class="post-metadata">

**Author:** ![DuongQuocViet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/duongquocviet/32/102893_2.png) [@DuongQuocViet](https://forum.freecodecamp.org/u/DuongQuocViet)\
**Post date:** [October 15, 2018, 4:06am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/5 "2018-10-15T04:06:31Z")

</div>

thanks @JordanMarsh but my function is not working i don’t where is wrong!

---

<div class="post-metadata">

**Author:** ![shimphillip](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shimphillip/32/158663_2.png) [@shimphillip](https://forum.freecodecamp.org/u/shimphillip)\
**Post date:** [October 15, 2018, 4:18am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/6 "2018-10-15T04:18:04Z")

</div>

Can you paste your changed code?

---

<div class="post-metadata">

**Author:** ![DuongQuocViet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/duongquocviet/32/102893_2.png) [@DuongQuocViet](https://forum.freecodecamp.org/u/DuongQuocViet)\
**Post date:** [October 15, 2018, 4:19am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/7 "2018-10-15T04:19:31Z")

</div>

this is last version

```auto
<script>
    function addColor () { 

      var udata = document.getElementById("centerbox1").value;

      if (udata > 0) {
      document.getElementById("centerbox1").style.color = '#99C262';
      }
      else  
      if (udata < 0)  
      {
      document.getElementById("centerbox1").style.color = '#FF6C60';
      }  
      else  
      if (udata === 0)  
      {
      document.getElementById("centerbox1").style.color = '#1456a8';
      }
   };
</script>

```

---

<div class="post-metadata">

**Author:** ![JordanMarsh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jordanmarsh/32/102627_2.png) [@JordanMarsh](https://forum.freecodecamp.org/u/JordanMarsh)\
**Post date:** [October 15, 2018, 4:24am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/8 "2018-10-15T04:24:05Z")

</div>

Sorry I hadn’t actually looked over the code much.

So you have a few problems. The first is you can’t access the value of any element except input elements. And you need to target it directly. So you would have to change your

```auto
<span>1</span>

```

to

```auto
<input id="centerbox1" value = 1>

```

make sure to remove the id from your div when doing this to avoid errors

then in your JavaScript you have a function but you never call it. So above  
`function addColor () {`

you would want to call  
`addColor();`

alternatively you could convert it to ES6 and do:  
`const addColor = () => {function here};`

Otherwise it should work. To save yourself some computing time you can save the element like I showed above and reference it directly instead of re-calling the DOM everytime.

```auto
let element = document.getElementById(“centerbox1”);
element.style.color = "somecolor";
element.style.backgroundColor = "somecolor";

```

---

<div class="post-metadata">

**Author:** ![DuongQuocViet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/duongquocviet/32/102893_2.png) [@DuongQuocViet](https://forum.freecodecamp.org/u/DuongQuocViet)\
**Post date:** [October 15, 2018, 4:37am UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/9 "2018-10-15T04:37:57Z")

</div>

Finally my function is working ! Thank you for spending time with me!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:09pm UTC](https://forum.freecodecamp.org/t/how-can-i-change-color-text-when-value-less-than-0/233835/10 "2021-01-17T20:09:39Z")

</div>

Closed on January 17, 2021, 8:09pm UTC


