...

View Full Version : Simple Calculations Form not working



spizer
01-24-2006, 07:25 AM
Hi there,

Just have a very simple script that im having issues with. I am a newb to Javascript, and cut bits and pieces to form this code.

Any help in making this work, will be greatly appreciated and good karma will come to you!!!!

Bascially, I need the output to Add up all the 4 fields.



<html>
<head>
<SCRIPT LANGUAGE="Javascript" TYPE="text/javascript">
<!--
function assetbank(bank1,bank2,bank3,bank4)
{
var totalbank = Math.round(bank1 + bank2 + bank3 + bank4);
document.calculator.totalassetbank.value = totalbank;
}
//-->
</SCRIPT>
</head>
<body>
<form name="calculator">
<p>
<input type=name="bank1">
</p>
<p>
<input type=name="bank2">
</p>
<p>
<input type=name="bank3">
</p>
<p>
<input type=name="bank4">
</p>
<p>
<input type=button value="Subtotal" onclick="assetbank(document.calculator.bank1.value,document.calculator.bank2.value,document.calculator.bank3. value,document.calculator.bank4.value);">
<textarea name="totalassetbank"></textarea>
</p>
</form>
</body>
</html>

glenngv
01-24-2006, 07:39 AM
"+" operator has 2 functions, addition and string concatenation. All field values are strings so they will be concatenated and not added. Use Number() method to convert each value to number.

var totalbank = Number(bank1) + Number(bank2) + Number(bank3) + Number(bank4);

spizer
01-24-2006, 08:24 AM
Hi Glenn, thanks so much for having a look for me.

That makes sense, however I've tried it and it still doesnt work.

The output is still blank, can you see anything else wrong with the script?

glenngv
01-24-2006, 09:01 AM
This is wrong:

<input type=name="bank1">

Should be:

<input type="text" name="bank1">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum