Repository navigation
Expand file tree
/
Copy pathapi_currency.html
More file actions
72 lines (55 loc) · 2.16 KB
/
Copy pathapi_currency.html
File metadata and controls
72 lines (55 loc) · 2.16 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch Name from API json</title>
</head>
<body>
<h1>Currency Names and Values</h1>
<select id="currency" onchange="change()">
<option value="select">Select please</option>
</select>
<div id="currency-namess"></div>
<script>
var baseCurrencyz = 'USD';
function change()
{
const select = document.getElementById('currency');
baseCurrencyz = select.options[select.selectedIndex].value;
fetchCurrencyRates();
alert(`Selecting currency is : ${baseCurrencyz}`);
};
async function fetchCurrencyRates() {
//const apiKey = 'YOUR_API_KEY';
const apiUrl = `https://v6.exchangerate-api.com/v6/8198d32f78ad10428b9e8395/latest/${baseCurrencyz}`;
const selectElement = document.getElementById('currency');
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data); // Log the entire response for debugging
const rates = data.conversion_rates;
let htmlContent = '';
for (const [currency, rate] of Object.entries(rates)) {
htmlContent += `<p>Currency: ${currency}, Rate: ${rate}</p>`;
}
for (const currency in rates) {
const option = document.createElement('option');
option.value = currency;
option.textContent = `${currency}}`;
selectElement.appendChild(option);
}
document.getElementById('currency-namess').innerHTML = htmlContent;
} catch (error) {
console.error('Error fetching data:', error);
}
}
function Currencyt()
{
fetchCurrencyRates();
}
window.addEventListener('load', Currencyt);
</script>
</body>
</html>