/    Sign up×
Community /Pin to ProfileBookmark

adding form textboxes together

hi,
being relatively new to javascript i have come across a problem…picture this…..

function row(){
document.formsum.TOTAL1.value = document.formsum.QUANTITY1.value * document.formsum.UNITPRICE1.value – ((document.formsum.QUANTITY1.value * document.formsum.UNITPRICE1.value) * document.formsum.DISCOUNT1.value / 100)

ok, the above is (most likely) a long handed way of working out the total of a sum entered into said textboxes, so if the QUANTITY =1, the UNITPRICE=10, and the DISCOUNT=10(%), then the total would be 9. ok……..

next is a second row of the same thing, only numbered ‘2’….

document.formsum.TOTAL2.value = document.formsum.QUANTITY2.value * document.formsum.UNITPRICE2.value – ((document.formsum.QUANTITY2.value * document.formsum.UNITPRICE2.value) * document.formsum.DISCOUNT2.value / 100)

so we have 2 rows working out 2 sums……

the next bit is where it gets wierd!….

A=document.formsum.Total1.value
B=document.formsum.Total2.value
C=(A+?

document.formsum.SubTotal.value=C
}

i would have thought that that A+B would add the values of the two textboxes and put it in the SUBTOTAL textbox, instead it appends total2 to total1 (i.e. if total1 = 10 and total2 = 2, it does not produce 12, but 102 ’10 and 2′)

i thought this odd so i tried C=(A*? and it works perfectly, it multiplies the values of the 2 textboxes together, but upon changing the * back to a + it went back to appending the numbers, not adding them!!

what am i doing wrong???

many thanks

david

(ps apologies to all the scripters who cringed at the scripting!!)

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@js_prof_consMar 27.2005 — Ok, it's very simple. The + sign is not only the additive, but the concatenation, operator. A and B are the values of strings, so when the + is used, it is assumed that you want to concatenate them. Your best option is to use the Number() function to convert it to a numerical data type:C = Number(A) + Number(B);Or use the Number function surrounding the two references to the Total fields.
Copy linkTweet thisAlerts:
@boragoauthorMar 27.2005 — thanks JS Prof Cons, if you were near i'd wash your car for you or something!!
Copy linkTweet thisAlerts:
@js_prof_consMar 27.2005 — Wash my car? I've let that thing gather filth for years. Haha, clean car. That's OK, nothing need as a reward.
Copy linkTweet thisAlerts:
@boragoauthorMar 28.2005 — cheers and many thanks again.
×

Success!

Help @borago 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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