Advertisement
wa12rior

XML Classes Currencies Solution

Nov 28th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.37 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>Document</title>
  8.     <script>
  9.         function getAjax(url, success) {
  10.             var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
  11.  
  12.             xhr.onreadystatechange = function () {
  13.                 if (xhr.readyState > 3 && xhr.status === 200) {
  14.                    success(xhr.responseText);
  15.                 }
  16.             };
  17.  
  18.             xhr.open('GET', url);
  19.             xhr.send();
  20.             return xhr;
  21.         }
  22.  
  23.         function getSoapRequest() {
  24.             var url = 'http://www.jozefkapusta.teacher.sk/xml/HelloClientWsdl.php?operation=currency';
  25.             getAjax(url, function (data) {
  26.                 currenciesJson = JSON.parse(data)
  27.                 console.log(currenciesJson);
  28.                 rates = currenciesJson.rates;
  29.                
  30.  
  31.                 selectList = document.getElementById('to');
  32.                 selectListFrom = document.getElementById('from');
  33.                 option = document.createElement("option");
  34.                 option.value = 1;
  35.                 option.text = currenciesJson.base;
  36.                 selectListFrom.appendChild(option);
  37.  
  38.                 for (let index in rates) {
  39.                     option = document.createElement("option");
  40.                     option.value = rates[index];
  41.                     option.text = index;
  42.                     selectList.appendChild(option);
  43.                 }
  44.             });
  45.         }
  46.  
  47.         function calculate() {
  48.             selectList = document.getElementById('to');
  49.             userValue = parseInt(document.getElementById('number').value);
  50.             var value = selectList.options[selectList.selectedIndex].value;
  51.             document.getElementById('result').innerHTML = value*userValue;
  52.         }
  53.  
  54.         getSoapRequest();
  55.     </script>
  56. </head>
  57. <body>
  58.     <h1>Currencies calculator</h1>
  59.     <div id="demo">
  60.         <select name="from" id="from" disabled></select>
  61.         <select name="to" id="to"></select>
  62.         <input type="number" name="number" id="number">
  63.         <button onclick="calculate()">Calculate</button>
  64.     </div>
  65.     <h1>Result</h1>
  66.     <p id="result"></p>
  67. </body>
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement