View Full Version : Simple Calculations Form not working

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.

<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;
<form name="calculator">
<input type=name="bank1">
<input type=name="bank2">
<input type=name="bank3">
<input type=name="bank4">
<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>

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);

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?

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

<input type=name="bank1">

Should be:

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