Answers for "javascript sum table row values"

0

javascript sum table row values

$(function() {
  $("#subtotal").html(sumColumn(4));
  $("#total").html(sumColumn(5));
});

function sumColumn(index) {
  var total = 0;
  $("td:nth-child(" + index + ")").each(function() {
    total += parseInt($(this).text(), 10) || 0;
  });  
  return total;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table style="border-spacing: 10px;">
  <tr>
    <td>ClientName</td>
    <td>ItemName</td>
    <td>Quantity</td>
    <td>12</td>
    <td>34</td>
  </tr>
  <tr>
    <td>ClientName</td>
    <td>ItemName</td>
    <td>Quantity</td>
    <td>56</td>
    <td>78</td>
  </tr>
  <tr>
    <td>ClientName</td>
    <td>ItemName</td>
    <td>Quantity</td>
    <td>90</td>
    <td>12</td>
  </tr>
  <tr>
    <td colspan="3">Totals</td>
    <td id="subtotal"></td>
    <td id="total"></td>
  </tr>
</table>
Posted by: Guest on July-22-2021

Code answers related to "javascript sum table row values"

Code answers related to "Javascript"

Browse Popular Code Answers by Language