Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div>
    <input id="currency" onBlur="calcBitCoinValue()"/>
    <input id="bitcoin"/>
  </div>
</body>
</html>
 
/*
<?php
   $url = 'https://www.mercadobitcoin.net/api/btc/ticker';
   $result = file_get_contents($url);
   $result = json_decode($result, true);
?>
*/
let currentBitcoinValue = 0;
let currentCurrencyValue = 0;
function fetchBitcoinTickerJSON() {
  const url = "https://www.mercadobitcoin.net/api/btc/ticker";
  
  fetch(url)
    .then(response => response.json())
    .then(data => {
        currentBitcoinValue = data.ticker.buy;
     });
}
function init() {
  fetchBitcoinTickerJSON();
}
function calcBitCoinValue() {
  const bitCoin = document.getElementById("bitcoin");
  const realValue = parseFloat(document.getElementById("currency").value);
  
  bitCoin.value = realValue/currentBitcoinValue;
}
init();
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers