Adding up Rows with JQuery

Simple and sometimes unnecessary function to add table cells together and populate another with the total. Using jQuery because its easy and it works and we all love it.

$(document).ready(function() {
    var x = 0;
    $("td.num").each(function() {
        x += parseInt($(this).html());
    })
    $("td#total").html(x);
});

Where your table cells to add up are <td class=”num”> and the table cell that has the total is <td id=”total”>.

Leave a Reply

Your email address will not be published. Required fields are marked *