Simon Phoenix
Simon Phoenix

Reputation: 31

Coinmarketcap API Not Working

Below is an API that I have for coinmarketcap.com. I am trying to pull the current price and place that into a table. For some reason this code is not working and I can not figure out the error. Any help is greatly appreciated.

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
  <table>
    <tr>
      <th>Coin Price</th>
    </tr>
    <td id="my_cell"></td>
  </table>

  <script>
    $.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {                  
      for (var i = 0; i < data.length - 1; i++) {                    
        if (data[i].id == "pivx") {                          
          $("#my_cell").innerHTML = data[i].price_usd;                    
        }                  
      }            
    }    
  </script>
</body>
</html>

Upvotes: 2

Views: 3369

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337530

Your code has a couple of problems. Firstly, your JS is missing a closing ) on the $.get method. You should remember to format your code correctly, then it's almost impossible to miss syntax errors like that. Secondly, the td should be wrapped in a <tr> in the HTML.

The main issue however is that you're attempting to use innerHTML on a jQuery object when they do not have that property. Instead use html(), like this:

$.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {
  for (var i = 0; i < data.length - 1; i++) {
    if (data[i].id == "pivx") {
      $("#my_cell").html(data[i].price_usd);
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Coin Price</th>
  </tr>
  <tr>
    <td id="my_cell"></td>
  </tr>
</table>

Upvotes: 3

Related Questions