Make a weight/mass unit converter using JavaScript, Paste given code on your PHP WordPress’s file. Then use created tool on mobile or computer.
How to make Weight/mass Unit Converter
- Open cpanel then create page-weight_converter.php file.
- Then paste given two code and Save.
HTML Code –
<label for="mg">mg</label><input placeholder="miligram" type="number" name="" id="mg" oninput="convertToOuthers('mg',this.value)"> <button onclick="document.getElementById('mg').value = ''">Clear</button> <label for="gram">g</label><input placeholder="gram" type="number" name="" id="gram" oninput="convertToOuthers('gram',this.value)"> <button onclick="document.getElementById('gram').value = ''">Clear</button> <label for="kg">kg</label><input placeholder="kilogram" type="number" name="" id="kg" oninput="convertToOuthers('kg',this.value)"> <button onclick="document.getElementById('kg').value = ''">Clear</button> <label for="t">t</label><input placeholder="tonne" type="number" name="" id="t" oninput="convertToOuthers('t',this.value)"> <button onclick="document.getElementById('t').value = ''">Clear</button>
JavaScript Code –
<script> let mg =document.getElementById("mg"); let gram =document.getElementById("gram"); let kg =document.getElementById("kg"); let t =document.getElementById("t"); function mgToOther(val){ gram.value = val/1000; kg.value = val/1000000; t.value = val/1000000000; } function gramToOther(val){ mg.value = val*1000; kg.value = val/1000; t.value = val/1000000; } function kgToOther(val){ mg.value = val*1000000; gram.value = val*1000; t.value = val/1000; } function tToOther(val){ mg.value = val*1000000000; gram.value = val*1000000; kg.value = val*1000; } function convertToOuthers(convertFrom,value){ switch(convertFrom){ case "mg" : mgToOther (parseFloat(value)); break; case "gram" : gramToOther (parseFloat(value)); break; case "kg" : kgToOther (parseFloat(value)); break; case "t" : tToOther (parseFloat(value)); break; } } </script>
Note
If any formula was wrong, so give me a feedback from contact us section. Thank you for visiting.