tags:

views:

41

answers:

2

I have:

var rows = $table.find('tbody > tr').get();

This selects all rows. However, not all tables have thead and tbody explicitly defined, so I need to filter out any rows that have .children('th')

+4  A: 

EDIT: I'm assuming you wanted to filter out the rows that have <th> elements. If you wanted to end up with only those rows, then just get rid of the :not() part.


This will give you <tr> elements in the table that do not have a descendant <th>.

var rows = $table.find('tr:not(:has(th))').get();

Note that this will also give consideration to nested tables. If there will be nested tables with <th> tags, try this:

var rows = $table.find('tr:not(:has( > th))').get();

...which should limit the consideration of the <th> tags to immediate children.

patrick dw
Thanks Patrick! A very complete answer.
cf_PhillipSenn
@cf_PhillipSenn - You're welcome. :o)
patrick dw
A: 

Why not change that to:

var rows = $table.find('tr > th').get();

EDIT:

var rows = $table.find("tr:has(th)").get();
spinon
spinon - This would actually select the `<th>` elements. I think OP wants to select the `<tr>` elements.
patrick dw
Patrick you are right. My mistake misread. I will fix.
spinon