Click here to Skip to main content
15,882,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
$('#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 ;
    $generaltesttotalamt = ($generaltestprice + $generaltax_vat_amt);

What I have tried:

$generaltesttotalamt = ($generaltestprice + $generaltax_vat_amt);

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.
Updated 24-Nov-22 22:34pm

1 solution

You're adding strings, not numbers. You need to convert your inputs to numbers before performing arithmetic on them.
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[^]
Share this answer

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

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