Search code examples
javascriptjqueryangularangular-datatables

How to get sum of checkboxes checked column values dynamically using jQuery


Angular Datatable: How to get sum of checked values amount dynamically in Datatable.

Have one datatable and checkbox, once we check the checkboxes selected and need to sum of all selected checkbox amount. I tried the following code, getting values but unable to sum. If I uncheck checkbox need to reduce the unchecked box amount. Please suggest the issue.

 checkboxCheckAmountCount(info: any): void {

 var creditAmount = 0;
    var checkedCount = $("#firstTable input:checked").length;
    var amountAdd = false;
    for (var i = 0; i < checkedCount; i++) {
      if (info.index != "") {
        alert(info.index);
        creditAmount += parseFloat(info.index);
      } else {
        creditAmount = 0;
      }
     }
   $("#idCheckBoxLabel").text(creditAmount);
}

Stackblitz


Solution

  • The following is the function which works fine as per your requirement:

    checkboxCheckCount(info: any): void {
      var creditAmount = 0;
      var amountAdd = false;
      $("#firstTable input:checked").each(function() {
        if (info.index != "") {
          creditAmount += parseInt($(this).prop("value"));
        } else {
          creditAmount = 0;
        }
      });
      $("#idCheckBoxLabel").text(creditAmount);
    }