Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

each

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)
                });
like image 407
FiveTools Avatar asked Oct 05 '11 16:10

FiveTools


People also ask

How can get TD of TR in jQuery?

Approach 2: Use $('table tr:last') jQuery Selector to find the last element of the table. The 'table' in query looks for the table element then 'tr' is looking for all the rows in the table element and ':last' is looking for the last table row of the table.

How can get TD value using ID in jQuery?

$(document). ready(function(){ var r=$("#testing":row2). val(); alert(r); });


2 Answers

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/

like image 69
cobblers Avatar answered Sep 23 '22 14:09

cobblers


I used the nth-child...

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

Some good alternative solutions here though.

like image 30
FiveTools Avatar answered Sep 22 '22 14:09

FiveTools