To achieve this, we can make full use of thead
and tfoot
in the table
element. With minor changes, we have the following:
HTML
<table id="sum_table" width="300" border="1"> <thead> <tr> <th>Apple</th> <th>Orange</th> <th>Watermelon</th> </tr> </thead> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tfoot> <tr> <td>Total:</td> <td>Total:</td> <td>Total:</td> </tr> </tfoot> </table>βββββββββββββββ
This allows us to more specifically orient the elements that we want, that is, how many columns there are, and what a βcommonβ cell is.
Javascript
$(document).ready(function() { $('table thead th').each(function(i) { calculateColumn(i); }); }); function calculateColumn(index) { var total = 0; $('table tr').each(function() { var value = parseInt($('td', this).eq(index).text()); if (!isNaN(value)) { total += value; } }); $('table tfoot td').eq(index).text('Total: ' + total); }β
source share