-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcur.js
More file actions
50 lines (50 loc) · 1.68 KB
/
cur.js
File metadata and controls
50 lines (50 loc) · 1.68 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
48
49
50
let base_url = "https://api.frankfurter.app/latest?amount";
let dropdowns = document.querySelectorAll(".dropdown select");
let btn = document.querySelector(".btn");
let fromCode = document.querySelector(".from");
let toCode = document.querySelector(".to");
for(let select of dropdowns){
for(let code in countryList)
{
let newOption = document.createElement("option");
newOption.innerText = code;
newOption.value = code;
if(select.className === "from" && code === "USD"){
newOption.selected = "selected";
}else{
if(select.className === "to" && code === "INR"){
newOption.selected = "selected";
}
}
select.append(newOption);
select.addEventListener("change",(evt)=>{
updateFlag(evt.target);
})
}
}
const updateFlag = (element) =>{
let code = element.value;
let countryCode = countryList[code];
let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png`;
let image = element.parentElement.querySelector("img");
image.src = newSrc;
}
btn.addEventListener("click", async (evt)=>{
evt.preventDefault();
let amount = document.querySelector(".text");
let amtValue = amount.value;
if(amtValue == "" || amtValue < 0){
amtValue = 1;
amount.value ="1";
}
let url = `${base_url}&from=${fromCode.value}&to=${toCode.value}`;
console.log(url);
let response = await fetch(url);
console.log(response);
let data = await response.json();
let data1 = data.rates;
let rate = data1[toCode.value];
let result = amtValue * rate;
let div = document.querySelector(".msg");
div.innerText = result;
});