Update script.js
Browse files
script.js
CHANGED
@@ -12,7 +12,10 @@ async function convertCurrency() {
|
|
12 |
const rate = data.rates[toCurrency];
|
13 |
const convertedAmount = (amount * rate).toFixed(2);
|
14 |
|
|
|
15 |
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
|
|
|
|
16 |
convertBtn.style.display = 'none';
|
17 |
} catch (error) {
|
18 |
result.innerText = "Error fetching exchange rates!";
|
@@ -24,14 +27,18 @@ function swapCurrencies() {
|
|
24 |
const fromCurrency = document.getElementById('from-currency');
|
25 |
const toCurrency = document.getElementById('to-currency');
|
26 |
|
|
|
27 |
const temp = fromCurrency.value;
|
28 |
fromCurrency.value = toCurrency.value;
|
29 |
toCurrency.value = temp;
|
30 |
|
|
|
31 |
convertCurrency();
|
32 |
}
|
33 |
|
|
|
34 |
document.getElementById('amount').addEventListener('input', convertCurrency);
|
|
|
35 |
document.getElementById('from-currency').addEventListener('change', () => {
|
36 |
document.getElementById('convert-btn').style.display = 'block';
|
37 |
document.getElementById('result').innerHTML = '';
|
|
|
12 |
const rate = data.rates[toCurrency];
|
13 |
const convertedAmount = (amount * rate).toFixed(2);
|
14 |
|
15 |
+
// Display conversion result
|
16 |
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
17 |
+
|
18 |
+
// Hide convert button after conversion
|
19 |
convertBtn.style.display = 'none';
|
20 |
} catch (error) {
|
21 |
result.innerText = "Error fetching exchange rates!";
|
|
|
27 |
const fromCurrency = document.getElementById('from-currency');
|
28 |
const toCurrency = document.getElementById('to-currency');
|
29 |
|
30 |
+
// Swap currency values
|
31 |
const temp = fromCurrency.value;
|
32 |
fromCurrency.value = toCurrency.value;
|
33 |
toCurrency.value = temp;
|
34 |
|
35 |
+
// Automatically convert after swap
|
36 |
convertCurrency();
|
37 |
}
|
38 |
|
39 |
+
// Event listeners for input and currency change
|
40 |
document.getElementById('amount').addEventListener('input', convertCurrency);
|
41 |
+
|
42 |
document.getElementById('from-currency').addEventListener('change', () => {
|
43 |
document.getElementById('convert-btn').style.display = 'block';
|
44 |
document.getElementById('result').innerHTML = '';
|