-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (47 loc) · 1.52 KB
/
index.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 URL='https://raw.githubusercontent.com/TheArmagan/currency/main/api';
const select=document.querySelectorAll('.select');
for(let data of select){
for(code in countryList){
let newOp=document.createElement("option");
newOp.innerText=code;
newOp.value=code;
if(data.name==="from" && code==="USD"){
newOp.selected="selected";
}
if(data.name==="to" && code==="INR"){
newOp.selected="selected";
}
data.append(newOp);
}
data.addEventListener("change",(e)=>{
newFlag(e.target);
})
}
const but=document.querySelector('.submit');
const msg=document.querySelector('.convertMsg');
const fromCur=document.querySelector('.from select');
const toCur=document.querySelector('.to select');
const updateExchange=async ()=>{
let amount=document.querySelector('.amount input');
let amtVal=amount.value;
const curUrl=`${URL}/${fromCur.value}-to-${toCur.value}.json`;
let res=await fetch(curUrl);
let out=await res.json();
let exchangeRate=out["value"];
let ans=amtVal*exchangeRate;
msg.innerText=`${amtVal} ${fromCur.value} = ${ans} ${toCur.value}`;
}
const newFlag=(el)=>{
let code=el.value;
let curCode=countryList[code];
let newImgSrc=`https://flagsapi.com/${curCode}/flat/64.png`;
let img=el.parentElement.querySelector("img");
img.src=newImgSrc;
}
but.addEventListener('click',(e)=>{
e.preventDefault();
updateExchange();
})
window.addEventListener("load",()=>{
updateExchange();
})