JSFiddle - React, Tailwind, and code Playground

by askhflajsf

HTML

<h1>Simple <a href="http://preev.com/">preev.com</a> clone</h1>
<input id="btc" type="text" value="1" />
<label for="btc">BTC</label>
<input id="fiat" type="text" value="0" />
<label for="fiat"></label>
<!--<p><a href="http://stackoverflow.com/questions/26416149/display-bitcoin-value-with-jquery">Based on StackOverflow #26416149</a></p>-->

JavaScript

var fiatCurrency = "NOK",
  conversionObj;

function getConversionRate() {
  return $.ajax({
    dataType: "json",
    
    // Use reverse proxy due to lack of CORS (http://enable-cors.org/)
    
    // https://github.com/Rob--W/cors-anywhere
    
    url: "https://cors-anywhere.herokuapp.com/localbitcoins.com/bitcoinaverage/ticker-all-currencies/",
    success: function(data) {
   
      // Parse JSON for rates
        
      rate = data[fiatCurrency].rates.last;
    }
  });
}

// Initialize rate, then call conversion for initial display

getConversionRate().then(bitcoinToFiat);

function fiatToBitcoin() {
  var number = Number($("#fiat").val());
  $("#btc").val((number / rate).toFixed(4));
};

function bitcoinToFiat() {
  var number = Number($("#btc").val());
  $("#fiat").val((number * rate).toFixed(2));
}

$('input').keyup(function() {
  if ($(this).is('input#btc')) {
    bitcoinToFiat();
  } else if ($(this).is('input#fiat')) {
    fiatToBitcoin();
  }
});

$('input').click(function() {
  $(this).select();
});

// Copy fiat to HTML

$('label[for="fiat"]').html(fiatCurrency);