Today I will be teaching you how to create a Fahrenheit to Celsius converter with HTML, CSS and JavaScript
HTML and CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div>
<h1>Convert Fahrenheit to Celsius and vice versa</h1>
Convert to: <select name="conversionSelect" id="conversionSelect">
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
</select> <br>
Value: <input type="text" id="value"> <br>
<button type="button" id="calcBtn">Calculate</button>
<div>Result: <span id="resultDisplay"></span></div>
</div>
<script src="script.js"></script>
</body>
</html>
JavaScript
let valueToBeConverted = document.getElementById('value');
let conversionSelect = document.getElementById('conversionSelect');
let convertBtn = document.getElementById('calcBtn');
let outputDisplay = document.getElementById('resultDisplay');
convertBtn.addEventListener('click', calculate);
function calculate(){
let result = 0;
if(conversionSelect.value === 'fahrenheit'){
result = (parseFloat(valueToBeConverted.value) * 9/5) + 32;
outputDisplay.innerHTML = result.toFixed(2);
} else {
result = (parseFloat(valueToBeConverted.value) - 32) * 5/9;
outputDisplay.innerHTML = result.toFixed(2);
}
}
If you found this blog post helpful don’t forget to check out my other projects and if you want more content follow me on
YouTube: https://www.youtube.com/channel/UC2nxqB2usQIS2c8zwVZKWEQ
Instagram: https://www.instagram.com/coding_for_the_web/
Twitter: https://twitter.com/LearnWebCoding
Comments