0

I have a problem that I don´t know how to solve. The script below displays stocks and its current value. One thing i want to add is the color green if the stock has a positive value and red if the stock has a negative value.

How can change the color of the div "value" for each row depending on the current value?

$(document).ready(function stocks() {

  var value = "";
  var name = "";
  var substring = "-";

  $.getJSON("https://finance.google.com/finance/info?client=ig&q=TSLA,HM-B,AAPL&callback=?", function(json) {
    json.forEach(function(v) {

      //if(v.cp.includes(substring)){
      //}
      //
      //else{
      //} 

      name += v.t + "<br>";
      value += v.cp + " %" + "<br>";

      document.getElementById('name').innerHTML = name;
      document.getElementById('value').innerHTML = value;
    });
    setTimeout(stocks, 10000);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <div id="name" style="float:left ; padding-right:10px"></div>
  <div id="value" style="text-align:right; width:140px"></div>
</div>

3
  • 3
    What colour value? Do you mean myElement.style.color = '#ccc'? Commented Apr 19, 2017 at 8:35
  • With an if statement, and what evolutionxbox said. Commented Apr 19, 2017 at 8:39
  • Move the .innerHTML = ... parts out of the .forEach() "loop". No need to change the DOM for each element if only the value of name/value after the last element is relevant. Commented Apr 19, 2017 at 8:40

4 Answers 4

1

You can change background style of an element on page by using this construction:

document.getElementById('value').style.background = 'here is your color'.

So, you can just replace your

"document.getElementById('value').innerHTML = value;"

by this:

const valueElement = document.getElementById('value');
const color = (v.cp > 0) ? 'greeen' : 'red';
valueElement.innerHTML = value;
valueElement.style.background = color;

Instead of changing "background" property you are free to change any other (color, width etc)

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

Comments

0

you can define a variable color and wrap the value in a span and set the span the color according to the value

see the code bellow

$(document).ready(function stocks() {

  var value = "";
  var name = "";
  var substring = "-";

  $.getJSON("https://finance.google.com/finance/info?client=ig&q=TSLA,HM-B,AAPL&callback=?", function(json) {
      json.forEach(function(v) {

          //if(v.cp.includes(substring)){
          //}
          //
          //else{
          //} 

          var color = 'green';
          if (v.cp < 0) {
            color = 'red';
          }

          name += v.t + "<br>";
          value += '<span style="color:' + color + '">' + v.cp + " %" + "</span><br>";

          document.getElementById('name').innerHTML = name;
          document.getElementById('value').innerHTML = value;




      }); setTimeout(stocks, 10000);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <div id="name" style="float:left ; padding-right:10px"></div>
  <div id="value" style="text-align:right; width:140px"></div>
</div>

Comments

0

You can call yourElement.style.color and set it to green or red

var value = "";
var name = "";
var substring = "-";

var nameElement = document.getElementById('name');
var valueElement = document.getElementById('value');

$.getJSON("https://finance.google.com/finance/info?client=ig&q=TSLA,HM-B,AAPL&callback=?", function(json) {
  json.forEach(function(v) {
    name += v.t + "<br>";
    value += v.cp + " %" + "<br>";
     
    valueElement.style.color = v.cp < 0 ? "red" : "green";
    
    nameElement.innerHTML = name;
    valueElement.innerHTML = value;
  });
  setTimeout(stocks, 10000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

<div id="name"></div>
<div id="value"></div>

2 Comments

This won't work. You would need to style each element individually.
Try changing https://finance.google.com/finance/info?client=ig&q=TSLA,HM-B,AAPL&callback=? to https://finance.google.com/finance/info?client=ig&q=NDRO,HM-B,AAPL&callback=? Even positive number will get a red color
0

Just parseFloat the value v.cp if it's greater than zero, apply green else apply red. Check below example.

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  <script>
    $(document).ready(function stocks() {

      var value = "";
      var name = "";
      var color = "";

      $.getJSON("https://finance.google.com/finance/info?client=ig&q=NDRO,HM-B,AAPL&callback=?", function(json) {

        json.forEach(function(v) {
          if (parseFloat(v.cp) > 0)
            color = 'green';
          else
            color = 'red';

          name += v.t + "<br>";
          value += "<span style='color:" + color + "'>" + v.cp + " %</span><br>";

          document.getElementById('name').innerHTML = name;
          document.getElementById('value').innerHTML = value;

        });
        setTimeout(stocks, 10000);
      });
    });
  </script>
</head>

<body>
  <div>
    <div id="name" style="float:left ; padding-right:10px">
    </div>
    <div id="value" style="text-align:right; width:140px">
    </div>
  </div>
</body>

</html>

Comments

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.