-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
48 lines (45 loc) · 1.94 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
const base_url = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies";
const dropdowns = document.querySelectorAll(".dropdown select");
const btn = document.querySelector("form button");
const fromCurr = document.querySelector(".from select");
const toCurr = document.querySelector(".to select");
const msg = document.querySelector(".msg")
for(let select of dropdowns){
for(currCode in countryList){
let newOption = document.createElement("option");
newOption.innerText = currCode;
newOption.value = currCode;
if(select.name ==="from" && currCode==="USD"){
newOption.selected = "selected";
}
else if(select.name ==="to" && currCode==="INR"){
newOption.selected = "selected";
}
select.append(newOption);
}
select.addEventListener("change",(evt) =>{
updateFlag(evt.target);
})
}
const updateFlag = (element)=>{
let currCode = element.value;
let countryCode = countryList[currCode]
let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png`
let img = element.parentElement.querySelector("img");
img.src = newSrc;
}
btn.addEventListener("click",async (evt)=>{
evt.preventDefault(); //The preventDefault() method cancels the event if it is cancelable, meaningthat the default action that belongs to the event will not occur.
let amount = document.querySelector("form input");
let amtValue = amount.value;
if(amtValue === "" || amtValue < 1){
amtValue = 1;
amount.value = "1"
}
const url = `${base_url}/${fromCurr.value.toLowerCase()}.json`;
let response = await fetch(url);
let data = await response.json();
let rate = data[fromCurr.value.toLowerCase()][toCurr.value.toLowerCase()] // data.usd("inr")
let finalAmount = amtValue * rate;
msg.innerText = `${amtValue} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`
})