How to create a weight converter with HTML and JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

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 −

raja
Published on 12-May-2020 17:11:05
Advertisements