To convert from centimeters to feet using JavaScript with HTML, you can create a simple HTML form where the user inputs the length in centimeters. Then, use JavaScript to perform the conversion and display the result dynamically.
Here’s an example:
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Centimeters to Feet Converter</title> </head> <body> <h2>Centimeters to Feet Converter</h2> <form id="converterForm"> <label for="centimeters">Enter Length in Centimeters:</label> <input type="text" id="centimeters" required> <input type="button" value="Convert" onclick="convertCentimetersToFeet()"> </form> <p id="result"></p> <script> function convertCentimetersToFeet() { // Get the length in centimeters from the input field var centimeters = parseFloat(document.getElementById("centimeters").value); // Perform the conversion var feet = centimeters / 30.48; // 1 foot = 30.48 centimeters // Display the result document.getElementById("result").innerHTML = centimeters + " centimeters is equal to " + feet.toFixed(2) + " feet."; } </script> </body> </html>
In this example:
– The HTML form contains an input field for the length in centimeters and a button to trigger the conversion.
– The `convertCentimetersToFeet` JavaScript function is called when the button is clicked.
– The length in centimeters is retrieved from the input field, and the conversion is performed.
– The result is dynamically displayed in a paragraph with the id “result”.
This is a basic example, and you might want to enhance it with additional validation to ensure the user input is valid.