Using jquery to find all td in a table by column position

I am trying to add a class to the last cell in each row of the table ... this does not work ... it applies only 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)
                });
+5
source share
6 answers

I used nth-child ...

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

Some good alternative solutions here.

+2
source

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/

+13
source

tds . . :

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

, lastIndex. :

rows.each(function () {
    $(this).children("td").last().addClass(rightStyle)
});
0
$("table.scrollable tr").each(function () {
                    $(this).children("td").eq(lastIndex).addClass(rightStyle);
                });
0

, , td

// SET FIRST AND LAST TD SIZE
$("tr").each(function() {
    $(this).children("td:last").attr("width", 200);
});
0

All Articles