PDA

View Full Version : javascript calculation problem

gorikain
12-10-2006, 02:50 AM
I can't figure this out. Sometimes a second set of eyes is helpful, so I figured I'd see what you guys think.

The purpose of this is to take five numbers, generate tax, choose a shipping method, and then finally combine tax, cost, and shipping.

The last step is what I can't get working.

Here's my code... any help would be appreciated.
PS: I put asterisks around the bit of code that I can't get working... for easy reference.

<html>

<script>
function runTotal() {

document.myform.total.value =(eval(document.myform.field1.value)
+ eval(document.myform.field2.value)
+ eval(document.myform.field3.value)
+ eval(document.myform.field4.value)
+ eval(document.myform.field5.value)).toFixed(2);
Tax = (document.myform.total.value * .06 ).toFixed(2);
document.myform.tax.value = Tax;

ship = eval(document.myform.ship.value);
cost = eval(document.myform.total.value);
***document.myform.grandtotal.value = eval(ship + Tax + cost).toFixed(2);***

}

</script>
<body>
<form name="myform">

<input type ='text' name = 'field1' value="0.00"> <br />
<input type ='text' name = 'field2' value="0.00"> <br />
<input type ='text' name = 'field3' value="0.00"> <br />
<input type ='text' name = 'field4' value="0.00"> <br />
<input type ='text' name = 'field5'value="0.00"> <br />
<input type ='text' name = 'total' readonly>Total <br />
<input type ='text' name = 'tax'>6% <br />
<input type="radio" name = 'ship' value="25" checked onClick="document.myform.shipping.value = 25.00"> Next Day <br />
<input type="radio" name = 'ship' value="10" onClick="document.myform.shipping.value = 10.00"> USPS <br />
<input type="radio" name = 'ship' value="5" onClick="document.myform.shipping.value = 5.00"> Mule<br />
<input type="text" name = "shipping" value="25">Shipping <br />

<input type ='text' name = 'grandtotal'>Grand Total <br />

<input type= "button" value= "Calculate Total"
onClick= "runTotal();">

</form>
</body>
</html>

david_kw
12-10-2006, 03:17 AM
Try this

function runTotal() {
document.myform.total.value = parseFloat(document.myform.field1.value)
+ parseFloat(document.myform.field2.value)
+ parseFloat(document.myform.field3.value)
+ parseFloat(document.myform.field4.value)
+ parseFloat(document.myform.field5.value);
Tax = parseFloat(document.myform.total.value) * .06;
document.myform.tax.value = Tax.toFixed(2);

ship = parseFloat(document.myform.shipping.value);
cost = parseFloat(document.myform.total.value);
document.myform.grandtotal.value = (ship + Tax + cost).toFixed(2);
}

david_kw

gorikain
12-10-2006, 03:38 AM
ah, that works flawlessly. Thanks :D