javascript - Need help updating input field on selecting dropdown option -


i'm building currency exchange app , i've got basic functionality done problem if change target currency while not changing base currency input value, target currency doesn't update. i'd when change value in second element automatically recalculates rate based on in base input.

this first time asking question on so gentle.

thank you.

you can find project here: https://codepen.io/azdravkovski/pen/bddleq?editors=0010

    $(document).ready(function () {      //first ajax call populates dropdown     $.ajax({         url: 'https://api.fixer.io/latest?',         success: function(data) {             var currencynobase = object.keys(data.rates);             var currencywithbase = currencynobase.concat(data.base).sort();             //populate dropdown lists currency names             $.each(currencywithbase, function (val, text) {                 $('.currencies').append($('<option />').val(val).html(text));             });             //set default currencies in base , target             $('#base').val('8');             $('#target').val('30');         },         datatype: 'json'     });      //return selected base currency     $('#curr1').on('input', function() {         var base = $('#base option:selected').text();         var url = 'https://api.fixer.io/latest?base=' + base;  //      second ajax call sets base currency         $.ajax({             url: url,             success: function(data2) {                 var target = $('#target option:selected').text();                 var curr1 = $('#curr1').val();                 var curr2 = $('#curr2').val();                 var converted = curr1 * data2.rates[target];                  $('#curr2').val(converted.tofixed(2));                 console.log(converted);                  //clear input fields when changing currency                 $('#base').on('change', function() {                     $('#curr1').val('');                     $('#curr2').val('');                 })             },             datatype: 'json'         });      }); 

and html:

    <!doctype html> <html lang="en">  <head>     <meta charset="utf-8">     <title>currency exchange rate app</title>     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rickshaw/1.6.1/rickshaw.min.css"> </head>  <body>      <!--interface-->     <select name="base" class="currencies" id="base"></select>     <input type="number" name="curr1" min="0" id="curr1" placeholder='0' step='.1'>     <br>     <br>     <select name="target" class="currencies" id="target"></select>     <input type="number" name="curr2" min="0" id="curr2" placeholder='0' disabled>     <!--    <button id="convert">convert</button>-->     <div id="test"></div>     <br>     <br>     <!--chart-->     <div id="chart"></div>      <!--scripts-->      <script src="https://d3js.org/d3.v3.min.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/rickshaw/1.6.1/rickshaw.js"></script>     <script type="text/javascript" src="vendor/jquery-3.2.1.js"></script>     <script type="text/javascript" src="src/app.js"></script>  </body>  </html> 

your code largely there, have added jquery 'change' event target currency option , by-and-large duplicated second ajax call code. results in additional behaviour understand wanted.

js fiddle: https://jsfiddle.net/2tokoeka/lyxwkux1/

additional code:

// event recognition target currency option change   $('select#target').on('change', function() {     var base = $('#base option:selected').text();     var url = 'https://api.fixer.io/latest?base=' + base;    // second ajax call sets base currency     $.ajax({         url: url,         success: function(data2) {             var target = $('#target option:selected').text();             var curr1 = $('#curr1').val();             var curr2 = $('#curr2').val();             var converted = curr1 * data2.rates[target];              $('#curr2').val(converted.tofixed(2));             console.log(converted);         },         datatype: 'json'     });   }); 

in terms of consolidating code when you're ready, make second ajax call separate function , call each event , extract code section clears code when base currency changed own block. shown in js-fiddle linked above.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -