Hello Friends Today, through this tutorial, I will tell you How can i Convert From Feet to Kilometers Using Javascript Code With HTML.
Here’s the HTML and JavaScript code for a Meters to Kilometers converter:
mtokm.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Meters to Kilometers Converter</title> </head> <body> <h1>Meters to Kilometers Converter</h1> <p>Enter a value in meters:</p> <input type="number" id="metersInput" placeholder="Meters"> <button onclick="convertMetersToKm()">Convert</button> <p id="result"></p> <script> function convertMetersToKm() { const meters = document.getElementById("metersInput").value; const kilometers = meters / 1000; // Conversion factor const result = document.getElementById("result"); result.textContent = `${meters} meters is equal to ${kilometers.toFixed(2)} kilometers.`; } </script> </body> </html>
Explanation:
1. The HTML code defines the basic structure of the page with headings, input fields, a button, and a paragraph element to display the result.
2. The JavaScript code is linked in a separate file named `script.js`.
3. The `convertMetersToKm` function is defined.
4. It retrieves the value entered in the `metersInput` field using `document.getElementById`.
5. It converts the meters to kilometers by dividing by 1000 (1 kilometer is equal to 1000 meters).
6. It retrieves the `result` element where the converted value will be displayed.
7. It uses template literals and string interpolation to format the output and display the converted value with two decimal places using `toFixed(2)`.
This code demonstrates a simple and functional way to convert meters to kilometers using HTML and JavaScript.