How do I add instead of concatenating [duplicate]

2

I would like some help.

I have the following code:

 var totalBruto = 0;
        var tot_desconto_vlr = 0;
        var total = 0;
        var totalLiquido = 0;
        $(document).ready(function () {
            $(".desc_vlr").on("input", function () {
                totalBruto = $("#tot_bruto").val();
                tot_desconto_vlr = $(this).val();

                total = totalBruto + tot_desconto_vlr;
                console.info(total);

               var tot_liquido = $(this).attr("desconto");

                $("#" + tot_liquido).val(total);
            });
        });

Instead of adding, it is concatenating example: 2 + 2 = 22

I would like to know what I'm doing wrong so I can add instead of concatenating.

Right now, thank you !!

    
asked by anonymous 31.08.2017 / 16:45

1 answer

1

He thinks you have a string. Try:

total = parseInt(totalBruto) + parseInt(tot_desconto_vlr);
    
31.08.2017 / 16:46