PDA

View Full Version : Adding form values together


pinkotoad
07-16-2002, 09:09 PM
Here is a small example.

function ComputeTotal( form )
{
CPU = form.CPU.value
MOB = form.Mainboard.value

subtotal = CPU + MOB

}

This doesn't add the two values, just jams them together.

Forgive me, I haven't done much JavaScripting.:o

HappyDude
07-16-2002, 09:21 PM
I believe that your problem is that form values are actually strings, even if they're just strings of numbers, so when you add them, they are being treated as strings and being concatenated(sp). The solution is to use parseInt() on the values before adding them...

CPU = parseInt(form.CPU.value)
MOB = parseInt(form.Mainboard.value)

subtotal = CPU + MOB

tamienne
07-16-2002, 10:27 PM
I don't think you get a decimal number if you use parseInt though. You can try Number...
CPU = Number(form.CPU.value)
MOB = Number(form.Mainboard.value)