Home > Back-end >  Jquery API value not working in input textarea
Jquery API value not working in input textarea

Time:09-02

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,litecoin,ethereum&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>

CodePudding user response:

<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,litecoin,ethereum&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>

CodePudding user response:

to change the value of the input tag you have to use btc.value instead of btc.innerHTML

 btc.value = response.bitcoin.usd;

  • Related