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:
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

1 solution

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[^]
 
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