Click to See Complete Forum and Search --> : easy question (I hope)


dillpickle79
11-05-2003, 08:23 PM
okay, I'm an asolute beginner with javascript, though I've known the language for two years. This is the first time I've ventured as far as using it. I currently am working on a simple form for a web site that calculates 2 selections and adds together their values. I've managed to display their values in text boxes, but the subtotal section just won't add them right. When I use the script, it treats the numbers as other languages would treat strings. eg: 100 + 100 is displayed as 100100. This is the script I am using basicaly to add the subtotals.

<select name="select3" onclick="javascript: document.form.subtotal.value = document.form.select3.value + document.form.select2.value">

seems simple enough but it doesn't work.
I'm also going to later on use multiplication and division, so I'm hoping that the magic solution that you give me will work for that too.

Thanks a lot!

Khalid Ali
11-05-2003, 08:42 PM
convert the type of strings to numbers by using

Number("100")+Number("150");

dillpickle79
11-05-2003, 09:01 PM
thanks a lot. Just one more question. I'm trying to apply two actions to the same onclick event in my form (as shown in my first post except fixed to use what Khalid Ali has kindly shown me to do. Now I need to add the value of the two selections and draw into a textbox, and with the same event draw into the selection b textbox.

thanks again,

AdamBrill
11-05-2003, 10:03 PM
Hmm... Is this what your looking for?

onclick="document.form.subtotal.value = parseFloat(document.form.select3.value) + parseFloat(document.form.select2.value); document.form.total.value = parseFloat(document.form.select3.value) + parseFloat(document.form.select2.value);">

As you might notice, I used parseFloat rather than Number. I think that is a better alternative, since it won't screw up if they enter a letter in the field.

dillpickle79
11-06-2003, 05:23 PM
thanks a lot for the help! that just helped out my web site a lot (well, it's not mine entirely but I'm a web techy at it...), check it out if you's like at www.cenproductions.net.

Thanks, I think I finaly figured out the answer to the question that scared me away from this in the first place.