[Javascript] Updating a text box - calculations

Paul Novitski paul at novitskisoftware.com
Mon Feb 14 00:37:37 CST 2005


At 10:21 PM 2/13/2005, Tim Burgan wrote:
>I have my 5 text fields.. that dollar values are entered into, then a 6th 
>box that calculates the total of all 5 boxes, but the values are not being 
>ADDED together for the totals box, instead the code is CONCATENATING. What 
>have I done, I can't figure it out?


Tim,

JavaScript uses the + sign for two very different operations, addition and 
concatenation.

2 + 2 == 4
"abc" + "def" == "abcdef"

If your numbers are concatenating instead of adding, it's because 
JavaScript is evaluating them as strings.  You can use parseInt() and 
parseFloat() to convert from string to numeric:

nTotal = parseInt(val1) + parseInt(val2) + ...

Paul 





More information about the Javascript mailing list