1

I have the following code which changes the background color of a table data cell according to the numerical value inside the cell.
While all other comparisons work as expected, (val<(-3000)) comparison never enters the block.
This is the code:

//Change Background Color for all P/L values
$(".PLcolors").each(function(index, value) {

  var val = Number(parseFloat($(this).text(), 10));

  console.log("value is " + val);

  if (val === 0) {
    $(this).css("background-color", "#DCDCDC");
  } else if ((val => -3000) && (val <= 3000)) {
    $(this).css("background-color", "#F0E68C");
  } else if (val < (-3000)) {
    $(this).css("background-color", "#FF0000");
  } else if ((val > 3000)) {
    $(this).css("background-color", "#008000");
  }
});

the type of val variable is number.

5
  • 2
    Please share fiddle for better clarity Commented Jul 14, 2017 at 11:49
  • 1
    What is the value of your val variable? Commented Jul 14, 2017 at 11:51
  • 2
    parseFloat takes only 1 argument. What gives you console.log($(this).text())? Commented Jul 14, 2017 at 11:51
  • @amoeba Sorry I cannot share a Fiddle since data are fetched live from a local webserver. Commented Jul 14, 2017 at 11:57
  • @Sergio The console gives back the value -4282 exactly without any additional characters. The operation runs for multiple values so the format does not seem to be the main issue since for positive numbers > 3000 the comparison and coloring is performed as expected. Commented Jul 14, 2017 at 11:59

1 Answer 1

2

There is an error inside your JS, you set => instead of >=. Fix it to:

else if (val >= -3000 && val <= 3000) {

//Change Background Color for all P/L values
$(".PLcolors").each(function(index, value) {

  var val = Number(parseFloat($(this).text(), 10));

  console.log("value is " + val);

  if (val === 0) {
    $(this).css("background-color", "#DCDCDC");
  } else if (val >= -3000 && val <= 3000) {
    $(this).css("background-color", "#F0E68C");
  } else if (val < -3000) {
    $(this).css("background-color", "#FF0000");
  } else if ((val > 3000)) {
    $(this).css("background-color", "#008000");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class="PLcolors">1</li>
  <li class="PLcolors">-3001</li>
  <li class="PLcolors">3001</li>
  <li class="PLcolors">0</li>
</ul>

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god! Such a small typo! Thanks for pointing it out! Issue solved everything works as expected now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.