-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (37 loc) · 1.41 KB
/
script.js
File metadata and controls
46 lines (37 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
document.addEventListener("DOMContentLoaded", () => {
let fromCurrency = document.getElementById("fromCurrency");
let toCurrency = document.getElementById("toCurrency");
let amountInput = document.getElementById("amount");
let result = document.getElementById("result");
let currencies = ["USD", "BDT", "EUR", "PKR", "INR", "AUD"];
currencies.forEach(currency => {
fromCurrency.innerHTML += `<option value="${currency}">${currency}</option>`;
toCurrency.innerHTML += `<option value="${currency}">${currency}</option>`;
});
fromCurrency.value = "USD";
toCurrency.value = "BDT";
convertBtn.addEventListener("click", () => {
let amount = amountInput.value;
let from = fromCurrency.value;
let to = toCurrency.value;
if (amount === "" || amount <= 0) {
result.innerText = "Please enter a valid amount";
return;
}
result.innerText = "Converting...";
fetch(`https://open.er-api.com/v6/latest/${from}`)
.then(res => res.json())
.then(data => {
if (!data.rates || !data.rates[to]) {
result.innerText = "Conversion failed";
return;
}
let rate = data.rates[to];
let converted = amount * rate;
result.innerText = `Converted Amount: ${converted.toFixed(2)} ${to}`;
})
.catch(() => {
result.innerText = "Error fetching data";
});
});
});