Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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>
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #sum #table #row #values
ADD COMMENT
Topic
Name
4+3 =