Getting The Last Column Of The Last Row Of A Table with jQuery

In Programming & Software Support by Jhong Regalado,
posted 4 years ago and was modified 4 years ago

Let's make it straight. You need to highlight the grand total of a report table. There are lot of approaches on how we can achive this. Let's assume that you already referenced jQuery in your HTML. First of all, we need to identify the table. Let's name it grandTotalObj

var grandTotalObj = $('#tblYTPLResult');

Then we need to get the last Table Row (tr), but first, we need to get the list of rows

var grandTotalObj = $('#tblYTPLResult')
        .find('tr');

Now that we have the list, let's get the last one

var grandTotalObj = $('#tblYTPLResult')
        .find('tr').last();

Same thing with the rows, we also need to get the list of Table Data (td) of the last row

var grandTotalObj = $('#tblYTPLResult')
        .find('tr').last()
        .find('td');

And finally, we may now get the last column

var grandTotalObj = $('#tblYTPLResult')
        .find('tr').last()
        .find('td').last();

Now that we got what we need, we need add the Grand Total text and highlight it

var grandTotalObj = $('#tblYTPLResult')
        .find('tr').last()
        .find('td').last();

grandTotalObj.prepend('Grand Total<br />');
grandTotalObj.wrapInner('<b></b>');

A working demo of this snippet can be found on my Youtube Playlist Calculator. We may always change this snippet according to our needs. Hope it helps.

Back To Programming & Software Support