Why? Learn fetching live exchange rates.
<input type="number" id="amount" placeholder="Enter amount">
<select id="currency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="PKR">PKR</option>
</select>
<button onclick="convert()">Convert</button>
<p id="result"></p>
<script>
async function convert() {
let amount = document.getElementById("amount").value;
let currency = document.getElementById("currency").value;
let res = await fetch(https://api.exchangerate-api.com/v4/latest/USD
);
let data = await res.json();
let rate = data.rates[currency];
document.getElementById("result").innerText = ${amount} USD = ${amount * rate} ${currency}
;
}
</script>
Explanation:
- Fetches real-time currency rates from API.
- Converts USD → selected currency.