shradha-khapra / JavaScriptSeries

257 stars 214 forks source link

fetch api is not working #26

Open Ridhi0730 opened 3 months ago

Ridhi0730 commented 3 months ago

const BASE_URL= "https://v6.exchangerate-api.com/v6/8da4da60cbe57dec9ced89c8/latest/USD"

const dropdown = document.querySelectorAll(".dropdown select");

const btn = document.querySelector("button");

const fromCurr = document.querySelector(".from select"); const toCurr = document.querySelector(".to select");

for (let select of dropdown) { for (let 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 updateExchangeRate = async () => { let amount = document.querySelector(".amount input"); let amtval = amount.value; if (amtval === "" || amtval < 1) { amtval = 0; amount.value = "0"; }

// console.log(fromCurr.value,toCurr.value);
const URL = `${BASE_URL}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json`;
let response = await fetch(URL);
    console.log(response);

}

const updateFlag = (element) =>{ let currcode = element.value; // console.log(currcode); countryCode = countryList[currcode]; let newsrc = https://flagsapi.com/${countryCode}/flat/64.png let img = element.parentElement.querySelector("img"); img.src = newsrc; }

btn.addEventListener("click", (evt) => { evt.preventDefault(); updateExchangeRate(); } )