15,882,927 members
1.00/5 (1 vote)
See more:
JavaScript
```\$('#ctrl-generaltestprice,#ctrl-generaltax_vat_per,#ctrl-generaltax_vat_amt').on('input', function(){
var \$sum =0;
var \$generaltestprice = \$(this).closest('form').find('#ctrl-generaltestprice').val();
var \$generaltax_vat_per = \$(this).closest('form').find('#ctrl-generaltax_vat_per').val();
var \$generaltax_vat_amt_dom = \$(this).closest('form').find('#ctrl-generaltax_vat_amt');
var \$generaltesttotalamt_dom = \$(this).closest('form').find('#ctrl-generaltesttotalamt');
\$generaltax_vat_amt = (\$generaltestprice * \$generaltax_vat_per)/100 ;
\$generaltax_vat_amt_dom.val(\$generaltax_vat_amt);
\$generaltesttotalamt = (\$generaltestprice + \$generaltax_vat_amt);
\$generaltesttotalamt_dom.val(\$generaltesttotalamt);
});```

What I have tried:

JavaScript
```\$generaltesttotalamt = (\$generaltestprice + \$generaltax_vat_amt);
\$generaltesttotalamt_dom.val(\$generaltesttotalamt);```

all function working but not working like addition(+) when this `\$generaltesttotalamt = (\$generaltestprice + \$generaltax_vat_amt);` looking like this 4+4 = 44 but I want to 4+4=8 so please help.
Posted
Updated 24-Nov-22 22:34pm
v2

## Solution 1

You're adding strings, not numbers. You need to convert your inputs to numbers before performing arithmetic on them.
JavaScript
```var \$generaltestprice = parseFloat(\$(this).closest('form').find('#ctrl-generaltestprice').val());
var \$generaltax_vat_per = parseFloat(\$(this).closest('form').find('#ctrl-generaltax_vat_per').val());
var \$generaltax_vat_amt_dom = parseFloat(\$(this).closest('form').find('#ctrl-generaltax_vat_amt'));
var \$generaltesttotalamt_dom = parseFloat(\$(this).closest('form').find('#ctrl-generaltesttotalamt'));```
parseFloat() - JavaScript | MDN[^]

If the values are entered by the user, you should also check that they are valid numbers - call `isNaN` on each parsed number to see if it's invalid.
isNaN() - JavaScript | MDN[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Graeme_Grant 95 Pete O'Hanlon 60 OriginalGriff 50 Maciej Los 40 raddevus 40
 Pete O'Hanlon 2,000 OriginalGriff 1,475 Graeme_Grant 1,030 Richard Deeming 893 Dave Kreskowiak 694

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900