views:

82

answers:

4

Is there a way in jQuery to tell how many cells are in a selected row? For example, I am iterating through the rows via:

$("#statsId > table tbody tr").each ( function() {
    var secondCol = $(this).find('td:nth-child(2)').html();
    var thirdCol= $(this).find('td:nth-child(3)').text();

and want to retrieve the second and third cell values. However when I do this iteration the cell values are coming back null when I know they are not. I thought if there was a way to verify the number of cells/columns in the selected row, I could at least verify that there are three cells in the selector. Or perhaps my selector for "secondCol" and "thirdCol" is wrong. Any help is appreciated.

+1  A: 
$('#myRow').children().size()

This is the most simple approach i can think of, but it's not infallable.

Paul Creasey
children allows for a filter selector, so you could do $('#row').children('td').size() and make sure you only get columns.
Juan
A: 
$("#statsId > table tbody tr").each(function() {
    var cells = $(this).find('td').size();
    ...
Tatu Ulmanen
+1  A: 
var currentRow = <jQuery object for current row>;
var currentRowCellCount = $('td', currentRow).length;

This simply gives you access to the total number of td elements in a given row.

Jon Cram
+1  A: 

You can use this to test:

if( $(this).children('td').length >= 3 ) {
   ...
}

But also play with using :eq() as well:

var secondCol = $(this).find('td:eq(1)').html();
var thirdCol= $(this).find('td:eq(2)').text();
Doug Neiner