|
async function convertCurrency() { |
|
const amount = document.getElementById('amount').value; |
|
const fromCurrency = document.getElementById('from-currency').value; |
|
const toCurrency = document.getElementById('to-currency').value; |
|
const result = document.getElementById('result'); |
|
const convertBtn = document.getElementById('convert-btn'); |
|
|
|
if (amount && fromCurrency && toCurrency) { |
|
try { |
|
const response = await fetch(`https://v6.exchangerate-api.com/v6/3ebe2ccf9eeea2aaef280201/latest/${fromCurrency}`); |
|
const data = await response.json(); |
|
|
|
if (data.result === "success") { |
|
const rate = data.rates[toCurrency]; |
|
const convertedAmount = (amount * rate).toFixed(2); |
|
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`; |
|
convertBtn.style.display = 'none'; |
|
} else { |
|
result.innerText = "Error: Unable to fetch exchange rates."; |
|
} |
|
} catch (error) { |
|
result.innerText = "Error fetching exchange rates!"; |
|
} |
|
} else { |
|
result.innerHTML = ''; |
|
convertBtn.style.display = 'block'; |
|
} |
|
} |
|
|
|
function swapCurrencies() { |
|
const fromCurrency = document.getElementById('from-currency'); |
|
const toCurrency = document.getElementById('to-currency'); |
|
|
|
|
|
console.log("Swapping currencies..."); |
|
console.log(`From: ${fromCurrency.value}, To: ${toCurrency.value}`); |
|
|
|
|
|
const temp = fromCurrency.value; |
|
fromCurrency.value = toCurrency.value; |
|
toCurrency.value = temp; |
|
|
|
|
|
convertCurrency(); |
|
} |
|
|
|
|
|
document.getElementById('amount').addEventListener('input', convertCurrency); |
|
|
|
document.getElementById('from-currency').addEventListener('change', () => { |
|
document.getElementById('convert-btn').style.display = 'block'; |
|
document.getElementById('result').innerHTML = ''; |
|
}); |
|
|
|
document.getElementById('to-currency').addEventListener('change', () => { |
|
document.getElementById('convert-btn').style.display = 'block'; |
|
document.getElementById('result').innerHTML = ''; |
|
}); |