Shorter code for adding checkboxes How can I make this code shorter?

function tblcheckboxes(){
    var a= 0;
    var b= 0;
    var c= 0;
    var d= 0;
    var e= 0;
    var f= 0;
    var g= 0;

    if ($('#tcbx1').is(":checked")) {
         a = parseFloat($("#tcbx1").val(), 10);
    }
    if ($('#tcbx2').is(":checked")) {
         b = parseFloat($("#tcbx2").val(), 10);
    }
    if ($('#tcbx3').is(":checked")) {
         c = parseFloat($("#tcbx3").val(), 10);
    }
    if ($('#tcbx4').is(":checked")) {
         d = parseFloat($("#tcbx4").val(), 10);
    }
    if ($('#tcbx5').is(":checked")) {
         e = parseFloat($("#tcbx5").val(), 10);
    }
    if ($('#tcbx6').is(":checked")) {
         f = parseFloat($("#tcbx6").val(), 10);
    }
    if ($('#tcbx7').is(":checked")) {
         g = parseFloat($("#tcbx7").val(), 10);
    }

    var total = a + b + c + d + e + f + g;
    $('.txt7').val(total.toFixed(2)); 
}

$(document).ready(function(){
        $('#tcbx1').click(function(){
                tblcheckboxes();
                grandtotal();
        });
        $('#tcbx2').click(function(){
                tblcheckboxes();
                grandtotal();
        });        
        $('#tcbx3').click(function(){
                tblcheckboxes();
                grandtotal();
        });
        $('#tcbx4').click(function(){
                tblcheckboxes();
                grandtotal();
        });
        $('#tcbx5').click(function(){
                tblcheckboxes();
                grandtotal();
        });
        $('#tcbx6').click(function(){
                tblcheckboxes();
                grandtotal();
        });
        $('#tcbx7').click(function(){
                tblcheckboxes();
                grandtotal();
        });
});
You could try to do the whole thing on the fly, like this:

var total = 0;
$("input.tcbx").change(function(){
    if($(this).is(":checked")) {
        total += parseFloat($(this).val(), 10);
    } else {
        total -= parseFloat($(this).val(), 10);
    }
    $('.txt7').val(total.toFixed(2)); 
});


Just add a class to all your checkboxes, and then either add the value to your total or reduce it as they get selected. If you have preselected values you might want to enter the preselected total already, otherwise you could use an each() to first check all the values.



$(document).ready(function(){
    var total = 0;
    $("input.tcbx").change(function(){
     if($(this).is(":checked")) {
      total += parseFloat($(this).val(), 10);
     } else {
      total -= parseFloat($(this).val(), 10);
     }
     $('#result').val(total.toFixed(2)); 
    });
});
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;input type="checkbox" value="1" id="check-1" class="tcbx" /&gt;
&lt;label for="check-1"&gt;1&lt;/label&gt;
&lt;input type="checkbox" value="2" id="check-2" class="tcbx" /&gt;
&lt;label for="check-2"&gt;2&lt;/label&gt;
&lt;input type="checkbox" value="3" id="check-3" class="tcbx" /&gt;
&lt;label for="check-3"&gt;3&lt;/label&gt;
&lt;input type="text" id="result"&gt;0&lt;/div&gt;