How to create a weight converter with HTML and JavaScript?



To create a weight converter with HTML and JavaScript, the code is as follows −

Example

 Live Demo

<!DOCTYPE html> <html> <head> <style>    body{       font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;    }    input,span{       font-size: 20px;    } </style> </head> <body> <h1>Weight Converter</h1> <h2>Type weight in kg to convert it into grams</h2> <p> <label>Kilogram</label> <input id="inputKG" type="number" placeholder="Kilogram" oninput="KgtoGConverter(this.value)" onchange="KgtoGConverter(this.value)"> </p> <p>Grams: <span id="outputGrams"></span></p> <script>    function KgtoGConverter(weight) {       document.getElementById("outputGrams").innerHTML=weight*1000;    } </script> </body> </html>

Output

The above code will produce the following output −

On typing some weight in KGs −

Updated on: 2020-05-12T13:41:39+05:30

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements