PDA

View Full Version : EASY - simply add fields


ashleypower
03-08-2003, 05:19 PM
I just wanna add 9 fields together! SIMPLE! But I can't do it! UGH! This is what I have:

atotal = (document.values.a.value) + (document.values2.a.value) + (document.values3.a.value) + ....etc.

Now, let's say that these are my field values:

1 2 3 4 5 6 7 8 9

When I send the total to another empty field, this is what it looks like:

123456789 instead of 45.

Please help!

COBOLdinosaur
03-08-2003, 05:25 PM
text inputs are a string and you have to convert tham to numbers or they will concatenate:

atotal = parseInt(document.values.a.value) + parseint(document.values2.a.value) + parseInt(document.values3.a.value) + ....etc.


If teh numbers have decimal points in them you have to use parseFloat instead of parseInt, but floating point arithmetic in JavaScript can produce errors.

Cd&