javascript - How to use data value to refer back to another variable -


i'm trying use create converter using data attribute refer previous value.

i cannot seem get:

  • data-my_currency refer 301.46
  • data-this refer 4259.00

$('input').keyup(function() {    var btc = 4259.00    var eth = 301.46    var ltc = 67.72      var usd = 1    var eur = 0.83    var cny = 6.46      var convertfrom = $('.js').data('my_currency');     //should refer eur var        var convertto = $('.js').data('this');     //should refer btc var        var amount = $('input').val();    var result = convertfrom * convertto * amount;          var newresult = result.tofixed(2);    $('.output').html(newresult);        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>      <div class="js" data-my_currency="eur"></div>  <div class="js" data-this="btc"></div>  <input type="text" class="input" />    <div class="output">me</div>

alternatively , without changing existing code much, can declare variables @ global scope (which should avoid) , use window object value of variable.

var btc = 4259.00  var eth = 301.46  var ltc = 67.72    var usd = 1  var eur = 0.83  var cny = 6.46    $(document).ready(function () {      $('input').keyup(function () {          var convertfrom = $('[data-my_currency]').data('my_currency');          var convertto = $('[data-this]').data('this');          var amount = $('input').val(); //assuming user enters numbers          var result = window[convertfrom] * window[convertto] * amount;          var newresult = result.tofixed(2);          $('.output').html(newresult);      });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  <div class="js" data-my_currency="eur"></div>  <div class="js" data-this="btc"></div>  <input type="text" class="input" />    <div class="output">me</div>


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -