logo image

How to write Distance converter program with jQuery + javascript + HTML

226


Demo

Code

<html>

<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script>
		$(document).ready(function() {
			$("#btn").click(function() {
				console.log("after click");
				var from = $("#cfrom").val();
				console.log("selected from value is " + from);

				var to = $("#cto").val();
				console.log("selected to value is " + to);

				var input = $("#value").val();
				console.log("selected input value is " + input);
				var output = -1;

				if (from == to) {
					alert("check your from & to values");
				}

				if (from == 'Meters' && to == 'Kilometers') {
					output = (+input) / 1000;
				} else if (from == 'Kilometers' && to == 'Meters') {
					output = ((+input) * (1000))
				} else if (from == 'Miles' && to == 'Kilometers') {
					output = ((+input) * (1.6))
				} else if (from == 'Kilometers' && to == 'Miles') {
					output = ((+input) / (1.6))
				} else if (from == 'Millimeters' && to == 'Centemeters') {
					output = ((+input) / (10))
				} else if (from == 'Centemeters' && to == 'Millimeters') {
					output = ((+input) * (10))
				} else if (from == 'Centemeters' && to == 'Meters') {
					output = ((+input) / (100))
				} else if (from == 'Meters' && to == 'Centemeters') {
					output = ((+input) * (100))
				}

				// 2 kilometers = 2000 meters
				output = input + " " + from + " = " + output + " " + to
				console.log("output value is" + output);
				$("#result").html(output);
			});
		});
	</script>
</head>

<body>

	Convert From :
	<select id="cfrom">
<option selected="selected"> Meters </option>
<option> Kilometers </option>
<option> Inches </option>
<option> Miles </option>
<option> Millimeters </option>
<option> Centemeters </option>
</select><br><br> Convert To :
	<select id="cto">
<option selected="selected"> Meters </option>
<option> Kilometers </option>
<option> Inches </option>
<option> Miles </option>
<option> Millimeters </option>
<option> Centemeters </option>
</select><br><br> Value:
	<input type="text" name="number" id="value"></input><br><br>
	<button id="btn"> Convert </button>
	<p id="result"></p>
</body>

</html>