Get row by index

how can you get a row by index?

var rows = $('tr', tbl); rows.index(0).addClass('my_class'); 
+7
source share
9 answers

Use .eq() .

 var rows = $('tr', tbl); rows.eq(0).addClass('my_class'); 

... or for your simple case .first() :

 rows.first().addClass('my_class'); 
+18
source

Using the eq() function:

 rows.eq(0).addClass('my_class'); 


Or :eq() selector:

 $('tr:eq(0)', tbl).addClass('my_class'); 
+6
source
 var row=$('tr:eq(5)', tbl); // returns the 5th row 
+4
source

You can do

 $('tr:eq(0)', tbl).addClass('my_class'); 

more about this http://api.jquery.com/eq-selector/

+3
source

You can use nth-child in your selector:

 $('tr td:nth-child(3)').addClass('my_class'); 

Get the third td.

+3
source

Use eq ()

 $('tr', tbl).eq(0).addClass('my_class'); 
+2
source

You can use the rows [docs] property on HTMLTableElement .

 $(tbl[0].rows[0]).addClass('my_class'); 

As @Felix noted, I suggested that tbl is a jQuery object. If not, do the following:

 $(tbl.rows[0]).addClass('my_class'); 
+2
source

For the first element (index 0), the answer to the previous question must be accurate.

For any nth element, use the eq selector

eg:

 var rows = $('tr:eq(8)', tbl); 
0
source

http://api.jquery.com/get/ says:

Get the DOM elements matching the jQuery object.
.get ([index])
index A zero-based integer indicating which item to retrieve.

Note that you will get a DOM object, not jQuery:

 var rows = $('tr', tbl); $(rows.get(0)).addClass('my_class'); 
0
source

All Articles