Advertise here

..Back

×

Unit converter example

Author: Win Aung Cho
2025-05-06 06:47:41AM

Unit converter example in jscript Save with ".html" file extension and open in browser.
	<form>
		<select onchange="n = this.value;">
		<option value="0">kPa-psf</option>
		<option value="1">ft-m</option>
		<option value="2">lb-kg</option>
		</select>
		<input id="quantity" type="text" placeholder="quantity in kPa" />
		<input type="button" value="Calculate" onclick="update()"/>
	</form>

	<!--display total cost here--> 
	<span>Equivalent quantity in psf</span>
	<script>
		var n = 0;
		var unit = [[20.88543, "kPa", "psf"], [0.3048, "ft", "m"], [0.453592, "lb", "kg"]];
		// Get DOMs

		var input = document.getElementById("quantity"),
			span = document.getElementsByTagName("span")[0];
		
		function update() {
			var val = parseFloat(input.value, 10); 
			var psf = (unit[n][0] * val);
			span.innerHTML = "Equivalent quantity " + `${val} ` + unit[n][1] + " = "+psf+" " + unit[n][2];
		}
	</script>


Author: Win Aung Cho

Comments and Answers

Recent Posts or Questions