/    Sign up×
Community /Pin to ProfileBookmark

Addition in javascript!!!

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

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@GollumNov 12.2003 — 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...
<i>
</i>var gt;
gt = parseInt(document.form.atot.value) + parseInt(document.form.ctot.value) + parseInt(document.form.sctot.value);
Copy linkTweet thisAlerts:
@shanuraguauthorNov 12.2003 — 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
Copy linkTweet thisAlerts:
@GollumNov 12.2003 — what are the values of your three fields?
Copy linkTweet thisAlerts:
@shanuraguauthorNov 12.2003 — Thanks a lot, I manage to get through the problem!!!]


shara
×

Success!

Help @shanuragu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...