how can you get a row by index?
var rows = $('tr', tbl); rows.index(0).addClass('my_class');
Use .eq() .
.eq()
var rows = $('tr', tbl); rows.eq(0).addClass('my_class');
... or for your simple case .first() :
.first()
rows.first().addClass('my_class');
Using the eq() function:
eq()
rows.eq(0).addClass('my_class');
Or :eq() selector:
:eq()
$('tr:eq(0)', tbl).addClass('my_class');
var row=$('tr:eq(5)', tbl); // returns the 5th row
You can do
more about this http://api.jquery.com/eq-selector/
You can use nth-child in your selector:
$('tr td:nth-child(3)').addClass('my_class');
Get the third td.
Use eq ()
$('tr', tbl).eq(0).addClass('my_class');
You can use the rows [docs] property on HTMLTableElement .
rows
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
$(tbl.rows[0]).addClass('my_class');
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);
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');