Click to See Complete Forum and Search --> : Addition in javascript!!!


shanuragu
11-12-2003, 04:34 AM
Hi

It sounds silly but I am struggling to add three values in js.

It is concatinating it & displaying it as a string. How can solve this problem. I am very bad in js.

How can I convert text box values in to integers??

var gt=0;

gt=document.form.atot.value+document.form.ctot.value+document.form.sctot.value;


shara

Gollum
11-12-2003, 04:54 AM
The result of the expression document.form.atot.value+document.form.ctot.value+document.form.sctot.value comes from the addition of three strings together which in javascript is another string. When you assign this value to your variable gt, gt will then become a string - javascript uses untyped variables so even though you declared gt with var gt=0; it is all forgotten on the next assignment.

you need to do this...

var gt;
gt = parseInt(document.form.atot.value) + parseInt(document.form.ctot.value) + parseInt(document.form.sctot.value);

shanuragu
11-12-2003, 05:00 AM
Not much dufference!!

gt=parseInt(document.form.atot.value) + parseInt(document.form.ctot.value)+ parseInt(document.form.sctot.value);
alert(gt);
document.form.gtot.value=gt;

gt value is NaN???

shara

Gollum
11-12-2003, 05:18 AM
what are the values of your three fields?

shanuragu
11-12-2003, 05:25 AM
Thanks a lot, I manage to get through the problem!!!]


shara