-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (35 loc) · 1.51 KB
/
app.js
File metadata and controls
47 lines (35 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function loadCurrencies(){
var from = document.getElementById('from');
var to = document.getElementById('to');
var xHttp = new XMLHttpRequest();
xHttp.onreadystatechange=function(){
if(xHttp.readyState == 4 && xHttp.status==200){
var obj = JSON.parse(this.responseText);
var options = '';
for(key in obj.rates){
options=options+ '<option>'+key+'</option>';
}
options += "<option>EUR</option>";
from.innerHTML = options;
to.innerHTML = options;
}
}
xHttp.open('GET','http://api.fixer.io/latest',true);
xHttp.send();
}
function convertCurrency(){
var from = document.getElementById("from").value;
var to = document.getElementById("to").value;
var xmlhttp = new XMLHttpRequest();
var url = 'http://api.fixer.io/latest?base='+ from+ '&symbols=' + to;
xmlhttp.open('GET',url,true);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var result = xmlhttp.responseText;
var jsResult = JSON.parse(result);
var amt = document.getElementById("fromAmount").value;
document.getElementById("toAmount").value = (jsResult.rates[to] * amt).toFixed(2);
}
}
}