Can anyone help pinpoint why my script won't work? Objective: user enters starting & ending miles along with gallons used to calculate mpg. Onchange events for first 3 input boxes call the calcMPG function.
Thx in advance!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calculate Gas Mileage</title>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
function calcMPG() {
var startMiles = parseInt(document.mpgform.startingMileage.value);
var endMiles = parseInt(document.mpgform.endingMileage.value);
var gallons = parseInt(document.mpgform.gallonsUsed.value);
var mpg = ((mpgform.endingMileage - mpgform.startingMileage)/mpgform.gallonsUsed);
}
if (isNaN(startingMileage.value) && isNaN(endingMileage.value) && isNaN(gallonsUsed.value)){
window.alert("You must enter a number");
}
else{
document.mpgform.milesPerGallon.value = mpg;
}
/* ]]> */
</script>
</head>
<body>
<form name="mpgform" action="">
<label>Starting mileage:</label>
<input type="text" name="startingMileage" value="0" onchange="calcMPG();"><br />
<label>Ending mileage:</label>
<input type="text" name="endingMileage" value="0" onchange="calcMPG();"><br />
<label>Gallons used:</label>
<input type="text" name="gallonsUsed" value="0" onchange="calcMPG();"><br />
<label>Miles per gallon:</label>
<input type="text" name="milesPerGallon" value="0";><br />
</form>
</body>
</html>