VijayKR
VijayKR

Reputation: 33

how to get the value from one function to another function

How to get the value from function A to functon B and alternatively? my code is like

var $quantities = $('input.PackingCharge');
$(".prdItems tr").toggle(function() {
    var totalQuantity = 0;
    $(this).attr('clickedItem', '1');

    //Calculate the TotalValu
    $quantities.each(function() {
        var GAtrr = (this.parentNode.parentNode);
        if (GAtrr.getAttribute('clickeditem') == '1') {
            var quantity = parseFloat(this.value);
            totalQuantity += quantity;
        }
    });
    // Total Value i hav append to the div and show the Total Value
    $('#materialsCast').html(parseFloat(totalQuantity).toFixed(2));
}, function() {
    // when i click this function the value not showing
    console.log(totalQuantity.toFixed(2) + 'After');
    // Wht are the item cliked  that i clike again the value need to subtration
    $quantities.each(function() {
        var GAtrr = (this.parentNode.parentNode);
        if (GAtrr.getAttribute('clickeditem') == '1') {
            var quantity = parseFloat(this.value);
            totalQuantity -= quantity;
        }
    });
});

Pls help me to subtracting the selected item when we are unselecting.

Upvotes: 0

Views: 702

Answers (1)

Nick Craver
Nick Craver

Reputation: 630379

You could simplify it overall by just getting the total of the clicked ones each time, like this:

$(".prdItems tr").click(function(){ 
    $(this).toggleClass('clicked');

    var totalQuantity = 0;                      
    $('.prdItems tr.clicked input.PackingCharge').each(function() {
      totalQuantity += parseFloat(this.value);
    });
    $('#materialsCast').html(parseFloat(totalQuantity).toFixed(2));     
});

Instead of a custom attributes, we're just using .toggleClass() to add/remove a clicked class on each click, and using that same class to select the "on" rows when calculating the total. The bonus here is you can now define a CSS class to go with it to add some styling for free, like this:

.clicked { background-color: green; }

Upvotes: 1

Related Questions