using jquery to find all td's in a table by column position

14,508

Solution 1

I used the nth-child...

   $("table.scrollable td:nth-child(" + lastIndex + ")").addClass(rightStyle);

Some good alternative solutions here though.

Solution 2

Do it all in one line...

$('table tr td:last-child').addClass(rightStyle);

// Targeting a particular column as pointed out by FiveTools
// Note that :nth-child(n) is 1-indexed
$('table tr td:nth-child(3)').addClass('highlight');

http://jsfiddle.net/cobblers/hWqBU/

Solution 3

When you search for tds, you need to look only inside the current row. See the addition of this:

            rows.each(function () {
                $("td:eq(" + lastIndex + ")", this).addClass(rightStyle)
            });
Share:
14,508

Related videos on Youtube

FiveTools
Author by

FiveTools

Updated on June 04, 2022

Comments

  • FiveTools
    FiveTools about 2 years

    I am trying to add a class to the last cell in each row of a table...this does not work...it only applies the rightStyle to the last element in the first (top) row...

    //the last cell in every row all have border right
                    var lastIndex = getLastVisibleIndex();
                    var rows = $("table.scrollable tr");
                    rows.each(function () {
                        $("td:eq(" + lastIndex + ")").addClass(rightStyle)
                    });