Search code examples
javascriptjquerybackbone.jscheckbox

Checkbox Check All unable to detect data


I'm having issues with retrieving details when checking the checkbox that ticks all the boxes. It works when i UNCHECK when it should be working when i CHECK.

When i Check it, i get Zero( by right should take sum of checked box data) When i UNcheck, i get Sum of all checked box data( by right should get zero)

I cant seem to "flip it" around or realise the logic here. Any advice would be greatly appreciated.

$(function() {
    var totalPrice = 0.00, 
        totalWeight = 0.00, 
        shippingFee = 0.00, 
        totalCost = 0.00, 
        agentFee = 0.00;

    updateTotals = function() {
        totalPrice = 0.00;
        totalWeight = 0.00;
        shippingFee = 0.00;
        totalCost = 0.00;
        agentFee = 0.00;

        $('#arrange-delivery-table tbody input[type=checkbox]:checked').each(function(i, e){
            totalWeight += Number($(this).data('weight'));
            totalCost += Number($(this).data('cost'));
        });
    };

    $('#arrange-delivery-table :checkbox').change(function(e){
        updateTotals();
    });
});

Solution

  • Try using click event:

    $('#arrange-delivery-table :checkbox').click(function(e){
        updateTotals();
    });