skip to Main Content

I am working in a crypto currency live price page. I am using Coingekho api for it. It is not working in
$<input type="number" id="bitcoin" value=""/> but it is woring in <h2 id="bitcoin"></h2>. I need it in <input type="number" id="bitcoin" value=""/>. Also I need all decimal value of price like $19755.25725.

var btc = document.getElementById("bitcoin");
var ltc = document.getElementById("litecoin");
var eth = document.getElementById("ethereum");

var liveprice = {
  "async": true,
  "scroosDomain": true,
  "url": "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Clitecoin%2Cethereum&vs_currencies=usd",

  "method": "GET",
  "headers": {}
}

$.ajax(liveprice).done(function(response) {
  btc.innerHTML = response.bitcoin.usd;
  ltc.innerHTML = response.litecoin.usd;
  eth.innerHTML = response.ethereum.usd;

});
    $<input type="number" id="bitcoin" value=""/>
    $<h2 id="litecoin"></h2>
    $<h2 id="ethereum"></h2>
<script  src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

2

Answers


  1. <input> elements don’t have innerHTML, since they’re not containers. You need to set btc.value.

    var btc = document.getElementById("bitcoin");
    var ltc = document.getElementById("litecoin");
    var eth = document.getElementById("ethereum");
    
    var liveprice = {
      "async": true,
      "crossDomain": true,
      "url": "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Clitecoin%2Cethereum&vs_currencies=usd",
    
      "method": "GET",
      "headers": {}
    }
    
    $.ajax(liveprice).done(function(response) {
      btc.value = response.bitcoin.usd;
      ltc.innerHTML = response.litecoin.usd;
      eth.innerHTML = response.ethereum.usd;
    
    });
        $<input type="number" id="bitcoin" value=""/>
        $<h2 id="litecoin"></h2>
        $<h2 id="ethereum"></h2>
    <script  src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    Login or Signup to reply.
  2. to change the value of the input tag you have to use btc.value instead of btc.innerHTML

     btc.value = response.bitcoin.usd;
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search