Sujay's Free Tools(currency converter)
Creating a complete currency converter tool with all currencies, colorful styling, and using the https://api.exchangerate-api.com library involves a considerable amount of code. I'll provide you with a simplified example using HTML, CSS, and JavaScript to get you started. Note that you'll need to sign up for an API key on https://www.exchangerate-api.com/ to use their service.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Currency Converter</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
text-align: center;
margin: 0;
padding: 0;
}
#converter {
max-width: 400px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
input {
width: calc(100% - 20px);
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#result {
font-weight: bold;
margin-top: 20px;
}
</style>
</head>
<body>
<div id="converter">
<h2>Currency Converter</h2>
<label for="amount">Amount:</label>
<input type="number" id="amount" placeholder="Enter amount">
<label for="fromCurrency">From Currency:</label>
<select id="fromCurrency">
<!-- Add currency options dynamically using JavaScript -->
</select>
<label for="toCurrency">To Currency:</label>
<select id="toCurrency">
<!-- Add currency options dynamically using JavaScript -->
</select>
<button onclick="convertCurrency()">Convert</button>
<div id="result"></div>
</div>
<script>
// Fetch currency options from the API
fetch('https://open.er-api.com/v6/latest')
.then(response => response.json())
.then(data => {
const currencies = Object.keys(data.rates);
const fromCurrencySelect = document.getElementById('fromCurrency');
const toCurrencySelect = document.getElementById('toCurrency');
currencies.forEach(currency => {
const option = document.createElement('option');
option.value = currency;
option.text = currency;
fromCurrencySelect.add(option);
const toOption = document.createElement('option');
toOption.value = currency;
toOption.text = currency;
toCurrencySelect.add(toOption);
});
});
function convertCurrency() {
const amount = document.getElementById('amount').value;
const fromCurrency = document.getElementById('fromCurrency').value;
const toCurrency = document.getElementById('toCurrency').value;
fetch(`https://open.er-api.com/v6/convert?from=${fromCurrency}&to=${toCurrency}&amount=${amount}`)
.then(response => response.json())
.then(data => {
const resultElement = document.getElementById('result');
resultElement.textContent = `${amount} ${fromCurrency} = ${data.conversion_result.toFixed(2)} ${toCurrency}`;
})
.catch(error => console.error('Error:', error));
}
</script>
</body>
</html>Remember to replace 'YOUR_API_KEY' with your actual API key from https://www.exchangerate-api.com/. This is a basic example, and you may want to enhance it further based on your specific requirements.
Comments
Post a Comment