-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (68 loc) · 2.35 KB
/
script.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const URL ="https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies";
const dropsel = document.querySelectorAll(".dropdown select");
const but=document.querySelector("button");
const fromcurr = document.querySelector(".from select");
const tocurr = document.querySelector(".to select");
const msg= document.querySelector(".msg");
for (let select of dropsel) {
for (currCode in countryList) {
let newOp = document.createElement("option");
newOp.innerText = currCode;
newOp.value = currCode;
if (select.name === "from" && currCode === "INR") {
newOp.selected = "selected";
}
else if (select.name === "to" && currCode === "USD") {
newOp.selected = "selected";
}
select.append(newOp);
}
select.addEventListener("change", (evt) => {
updateFlag(evt.target);
});
}
const updateFlag=(element)=>{
let crntcode =element.value;
let councode=countryList[crntcode];
let newimg=`https://flagsapi.com/${councode}/flat/64.png`;
let img=element.parentElement.querySelector("img");
img.src =newimg;
};
but.addEventListener("click",async(evt)=>{
evt.preventDefault();
let amount = document.querySelector(".amount input");
let amtval=amount.value;
if(amtval==="" || amtval<1){
amtval=1;
amount.value = "1";
}
const url =`${URL}/${fromcurr.value.toLowerCase()}/${tocurr.value.toLowerCase()}`;
let response = await fetch (url);
let data= await response.json();
let rate= data[tocurr.value.toLowerCase()];
let famt = amtval * rate;
console.log(famt);
msg.innerText = `${amtval} ${fromcurr.value} = ${famt} ${tocurr.value}`;
})
/*
const updateExchangeRate = async () => {
let amt =document.querySelector(".amount input");
let amtval=amt.value;
if (amtval=="" || amtval<1){
amtval=1;
amount.value=1;
}
const BURL = `${URL}/${fromcurr.value.toLowerCase()}/${tocurr.value.toLowerCase()}.json`;
let response = await fetch (BURL);
let data= await response.json();
let rate= data[tocurr.value.toLowerCase()];
let famt = amtval * rate;
msg.innerText = `${amtval} ${fromcurr.value} = ${famt} ${tocurr.value}`;
};
but.addEventListener("click",async (evt)=>{
evt.preventDefault();
updateExchangeRate();
});
window.addEventListener("load", () => {
updateExchangeRate();
}); */