-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
1 lines (1 loc) · 1.18 KB
/
script.js
1
const URL="https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies",dropDown=document.querySelectorAll(".select-container select"),btn=document.getElementById("btn"),fromSelect=document.querySelector("#select1"),toSelect=document.querySelector("#select2"),message=document.getElementById("converted-rate"),changeFlag=e=>{let t=e.value,n=countryList[t],c=`https://flagsapi.com/${n}/shiny/64.png`,a=e.parentElement.querySelector("img");a.src=c};for(let e of dropDown){for(let t in countryList){let n=document.createElement("option");n.value=t,n.innerText=t,"from"===e.name&&"USD"===t?n.selected="selected":"to"===e.name&&"INR"===t&&(n.selected="selected"),e.append(n)}e.addEventListener("change",e=>{changeFlag(e.target)})}const exchangeRate=async()=>{let e=document.querySelector("form input"),t=e.value;(t<1||""===t)&&(t=1,e.value="1");const n=`${URL}/${fromSelect.value.toLowerCase()}/${toSelect.value.toLowerCase()}.json`;let c=await fetch(n),a=await c.json(),l=a[toSelect.value.toLowerCase()],o=t*l;message.innerText=`${t} ${fromSelect.value} = ${o} ${toSelect.value}`};btn.addEventListener("click",e=>{e.preventDefault(),exchangeRate()}),window.addEventListener("load",()=>{exchangeRate()});