Browsing articles tagged with " javascript"

Adding up Rows with JQuery

Feb 26, 2009   //   by Mike McMurray   //   Code, Web Apps  //  No Comments

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”>.