Subtraction between variables of different functions

0

I would like to know how I could do a subtraction between the totalprice and total price2 variables and show it in the Lbtot label

Here my code:

                 $(function() {
                     $("input[type=checkbox]").change(function() {
                         var totalPrice = 0, ctlPrice;
                         $('.Gvcobranzas tr').each(function() {
                             if ($(this).find('input:checkbox').attr("checked")) {
                                 ctlPrice = $(this).find('[id$= Label3]');
                                 totalPrice += parseFloat(ctlPrice.text().replace(/[^\d\.]/g, ''));
                             }
                         });
                         $('.sum').text("$ " + totalPrice.toFixed(2));
                     });
                 });

                 $(function() {

                     $("input[type=checkbox]").change(function() {
                         var totalPrice2 = 0, ctlPrice2;
                         $('.GvNotasCredito2 tr').each(function() {
                             if ($(this).find('input:checkbox').attr("checked")) {
                                 ctlPrice2 = $(this).find('[id$= Label20]');
                                 totalPrice2 += parseFloat(ctlPrice2.text().replace(/[^\d\.]/g, ''));
                             }
                         });
                         $('.res').text("$ " + totalPrice2.toFixed(2));
                     });
                 });

             }
    
asked by otroklk8 28.08.2017 в 15:45
source

2 answers

0

Two details:

  • When you use $(function () { //mi codigo}) what you are doing is executing the code when the page is loaded and ready. with which the ideal thing is that all the code is within the same function.

  • Anyway, having two functions, one to obtain each value, is a good idea, but the ideal would be to call each one and have them return the value they calculate.

If you join these two ideas you can do the following:

$(function() {

    function getTotalPrice() {
        var totalPrice=0;
        $('.Gvcobranzas tr').each(function() {
            if ($(this).find('input:checkbox').attr("checked")) {
                var ctlPrice = $(this).find('[id$= Label3]');
                totalPrice += parseFloat(ctlPrice.text().replace(/[^\d\.]/g, ''));
            }
        });
        return totalPrice;
    }

    function getTotalPrice2() {
        var totalPrice2=0;
        $('.GvNotasCredito2 tr').each(function() {
            if ($(this).find('input:checkbox').attr("checked")) {
                var ctlPrice2 = $(this).find('[id$= Label20]');
                totalPrice2 += parseFloat(ctlPrice2.text().replace(/[^\d\.]/g, ''));
            }
        });
    }

    var totalPrice=getTotalPrice();
    var totalPrice2=getTotalPrice2();
    $('.sum').text("$ " + totalPrice.toFixed(2));
    $('.res').text("$ " + totalPrice2.toFixed(2));

    var difference=totalPrice - totalPrice2;
    $('#LbTot').text("$ "+difference.toFixed(2));
});
    
answered by 28.08.2017 / 16:22
source
0

Enclose the entire logic in anonymous method only. Then the variables totalPrice and totalPrice2 declare them outside the events change so that both events can access the variables. You create a button that by clicking on it, subtract the 2 values of the variables. Remember to create the event within the anonymous function also so that you can access the variables totalPrice and totalPrice2 :

$(function() {

    var totalPrice = 0;
    var totalPrice2 = 0;

     $("input[type=checkbox]").change(function() {
         var ctlPrice;
         totalPrice = 0;
         $('.Gvcobranzas tr').each(function() {
             if ($(this).find('input:checkbox').attr("checked")) {
                 ctlPrice = $(this).find('[id$= Label3]');
                 totalPrice += parseFloat(ctlPrice.text().replace(/[^\d\.]/g, ''));
             }
         });
         $('.sum').text("$ " + totalPrice.toFixed(2));
     });

     $("input[type=checkbox]").change(function() {
        var ctlPrice2;
        totalPrice2 = 0;
         $('.GvNotasCredito2 tr').each(function() {
             if ($(this).find('input:checkbox').attr("checked")) {
                 ctlPrice2 = $(this).find('[id$= Label20]');
                 totalPrice2 += parseFloat(ctlPrice2.text().replace(/[^\d\.]/g, ''));
             }
         });
         $('.res').text("$ " + totalPrice2.toFixed(2));
     });

     $("#restar").click(function() {
         alert(totalPrice - totalPrice2);// imprimes la resta
     });

 });
    
answered by 28.08.2017 в 16:06