Home » Jquery » How to update column data using jquery-Exceptionshub

How to update column data using jquery-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

This code is updating the column data but only for first time.When the second onchange event happens it is not working.

$('#pmselector, #docmselector').change(function() { //onchange events
  var result = [];

  $('#default_table tr').each(function() {
    $('td', this).each(function(index, val) {
      if (!result[index]) result[index] = 0;
      result[index] += parseInt($(val).text()); //summation of all the table data in a single column.
    });
  });

  $('#default_table').append("<tr></tr>"); //adding a new row to the table.
  var a = 1; //local variable
  $(result).each(function() { //function
    if (a === 1) {
      $('#default_table #mk').first().replaceWith('<td>Total</td>'); //replacing the first to last table data
      a++; //for the first time it will print Total in the first <td>
    } else {
      $('#default_table #mk').first().replaceWith('<td>' + this + '</td>'); //replacing the first to last table data
      a++;
    }
  });
});
How to&Answer: