Hello Friends Today, through this tutorial, I will tell you How to Convert inches to meters Using JavaScript With HTML. Here’s the HTML and JavaScript code for converting inches to meters:
inchestometers.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Inches to Meters Converter</title> </head> <body> <h1>Inches to Meters Converter</h1> <p>Enter a value in inches:</p> <input type="number" id="inchesInput" placeholder="Inches"> <button onclick="convertInchesToMeters()">Convert</button> <p id="result"></p> <script> function convertInchesToMeters() { const inches = document.getElementById("inchesInput").value; const meters = inches * 0.0254; // Conversion factor const result = document.getElementById("result"); result.textContent = `${inches} inches is equal to ${meters.toFixed(2)} meters.`; } </script> </body> </html>
Explanation-
1. Defines the basic structure with a heading, input field, button, and paragraph element to display the result.
2. Links the JavaScript code in a separate file named `script.js`.
3. Creates a function `convertInchesToMeters`.
4. Retrieves the value entered in the `inchesInput` field using `document.getElementById`.
5. Converts the inches to meters using the conversion factor (1 inch equals 0.0254 meters).
6. Retrieves the `result` element where the converted value will be displayed.
7. Uses template literals and string interpolation to format the output and display the converted value with two decimal places using `toFixed(2)`.