Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "Currency Convertor" project using HTML, CSS & JavaScript.
Preview
In the above video, you’ve seen the preview of the "Currency Convertor" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS, and JavaScript code below.
Currency Convertor [Source Code]
To get the following HTML, CSS & JS code for the Currency Convertor project. You need to create three files one is an HTML file, the second one is a CSS file and another one is a JS file. After creating these three files then you can copy-paste the given codes on your document.
Remember, you’ve to create a file with a .html extension for HTML code, a .css extension for CSS code, and .js for JavaScript code.
You can also download all source code files from the given download button.
<!DOCTYPE html>
<!-- Coding By CodingNepal - youtube.com/codingnepal -->
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Currency Convertor</title>
<link rel="stylesheet" href="style.css">
<!-- Font awesome cdn -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<h1>Currency Convertor</h1>
<div class="container">
<form action="#">
<div class="amount">
<p>Amount</p>
<input type="text" value="100" placeholder="Enter the amount">
</div>
<div class="FromToContainer">
<div class="from">
<p>From :</p>
<div class="selectBox">
<!-- You can use other countries currency options in given below-->
<select>
<option value="INR">India (INR)</option>
<option value="USD">United States (USD)</option>
<option value="AFN">Afghanistan (AFN)</option>
<option value="CNY">China (CNY)</option>
<option value="ARS">Argentine (ARS)</option>
</select>
</div>
</div>
<div class="to">
<p>To :</p>
<div class="selectBox">
<!-- You can use other countries currency options in given below-->
<select>
<option value="INR">India (INR)</option>
<option value="USD">United States (USD)</option>
<option value="AFN">Afghanistan (AFN)</option>
<option value="CNY">China (CNY)</option>
<option value="ARS">Argentine (ARS)</option>
</select>
</div>
</div>
</div>
<div class="convertedAmount">Click on "Convert Currency" Button</div>
<button class="convertBtn">Convert Currency</button>
</form>
</div>
<script src="index.js"></script>
</body>
</html>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
min-height: 100vh;
background: greenyellow;
}
h1 {
margin-bottom: 15px;
font-size: 40px;
text-transform: capitalize;
color: black;
word-spacing: 3px;
}
.container {
width: 350px;
padding: 20px;
border-radius: 10px;
background: white;
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.3);
}
form p {
font-size: 20px;
font-weight: 500;
text-transform: uppercase;
text-align: center;
margin-bottom: 5px;
}
form input {
font-size: 18px;
outline: none;
text-align: center;
width: 100%;
height: 50px;
padding: 0 20px;
border-radius: 5px;
border: 2px solid black;
}
form input:focus {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
form .FromToContainer {
display: flex;
margin: 25px;
align-items: center;
justify-content: space-between;
flex-direction: column;
gap: 10px;
}
.FromToContainer .selectBox {
display: flex;
width: 330;
height: 40px;
align-items: center;
border-radius: 5px;
justify-content: center;
border: 2px solid black;
}
.FromToContainer p {
text-align: start;
font-weight: 400;
}
.selectBox select {
width: 310px;
font-size: 18px;
border: none;
background: none;
text-align: center;
}
form .convertedAmount {
font-size: 18px;
margin: 15px;
text-align: center;
}
form button {
width: 100%;
font-size: 18px;
height: 50px;
color: black;
border: none;
background: greenyellow;
border-radius: 5px;
cursor: pointer;
}
form button:active {
transform: scale(0.95);
}
form button:hover {
filter: brightness(1.1);
}
const amountEl = document.querySelector("input");
const fromCurrencyEl = document.querySelector(".from select");
const toCurrencyEl = document.querySelector(".to select");
const convertedAmountEl = document.querySelector(".convertedAmount");
const convertBtnEl = document.querySelector(".convertBtn");
async function convertCurrencyFun(){
let inputAmount = amountEl.value;
convertedAmountEl.innerText = "Currency Converting...";
// The below API is Exchange rate API.
// This is available at https://www.exchangerate-api.com/
// After login with your email account. You will get your API key. You that api key in the place of "YOUR-API-KEY".
let url = `https://v6.exchangerate-api.com/v6/YOUR-API-KEY/latest/${fromCurrencyEl.value}`;
try{
const response = await fetch(url);
const data = await response.json();
let convertedAmount = data.conversion_rates[toCurrencyEl.value];
let totalConvertedAmount = (inputAmount * convertedAmount).toFixed(2);
convertedAmountEl.innerText = `${inputAmount} ${fromCurrencyEl.value} = ${totalConvertedAmount} ${toCurrencyEl.value}`;
}catch(e){
convertedAmountEl.innerText = "Oops... Error...!";
}
}
convertBtnEl.addEventListener("click", () =>{
convertCurrencyFun();
});