Convert from Kilometers to Feet Using Javascript code with HTML

Hello Friends Today, through this tutorial, I will tell you Convert from Kilometers to Feet Using Javascript script code with html.

Here’s the HTML and JavaScript code for a simple kilometers to feet converter:

HTML: km-to-feet.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kilometers to Feet Converter</title>
</head>
<body>
<h1>Kilometers to Feet Converter</h1>
<label for="km">Enter kilometers:</label>
<input type="number" id="km" placeholder="0">
<button onclick="convertKmToFeet()">Convert</button>
<p id="result"></p>
<script>
function convertKmToFeet() {
const kilometers = parseFloat(document.getElementById("km").value);
const feet = kilometers * 3280.84;
const resultElement = document.getElementById("result");
resultElement.textContent = `${kilometers} kilometers is equal to ${feet.toFixed(2)} feet.`;
}
</script>
</body>
</html>

Explanation:

1. The HTML code creates a basic structure with a heading, input field for kilometers, a button to trigger the conversion, and a paragraph to display the result.
2. The JavaScript code defines a function `convertKmToFeet()`.
3. The function retrieves the value entered in the kilometers input field using `document.getElementById(“km”).value`.
4. It converts the kilometers to feet using the formula `feet = kilometers * 3280.84`.
5. The result is then displayed in the paragraph element with `toFixed(2)` to format the number with two decimal places.