views:

45

answers:

2

Using only jQuery, how would I select the next rows in a table with an ID length > 0?

In my code, I'm using a hybrid of jQuery and javascript. And it works fine. However, I'd like it to be all in jQuery.

var rowsWithIds = new Array();
var rows = $(myTable).nextAll("tr");
for(var i=0; i < rows.length; i++)
{
    if(rows[i].id.length > 0)
        rowsWithIds[rowsWithIds.length] = rows[i];
}

Perhaps, the solution might look like:

var rowsWithIds = $(myTable).nextAll("tr").has("some selector");

or

var rowsWithIds = $(myTable).nextAll("tr and some selector");

Let me know. Thanks!

+2  A: 

The following gets all table-rows with an id, removing all that have an empty id:

var rowsWithIDs = $("tr[id]").not("[id='']");
Jonathan Sampson
Then the final answer is: var rowsWithIds = $(myTable).nextAll("tr[id]").not("[id='']");
Bill Paetzke
+2  A: 

change

var rows = $(myTable).nextAll("tr");

to

var rows = $(myTable).nextAll("tr[id]");

it will select all tr with id (you should not use id with no value)

Gaby
Perfect. Thanks, Gaby.
Bill Paetzke
Note, this will include `<tr id=''></tr>` too.
Jonathan Sampson
Touche! I did not test that scenario. Thanks, Jon.
Bill Paetzke
@Jonathan, that is true .. and hence the disclaimer that he should not use id without an actual value ..
Gaby