Phillip Senn
Phillip Senn

Reputation: 47595

jquery selector to filter out th elements

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')

Upvotes: 2

Views: 3022

Answers (2)

spinon
spinon

Reputation: 10847

Why not change that to:

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

EDIT:

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

Upvotes: -1

user113716
user113716

Reputation: 322462

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.

Upvotes: 11

Related Questions