Reputation: 31280
I want to identify all tables with more than 6 rows, as well as all tables with 6 or fewer rows.
About the best I can come up with is to loop over the rows for each table and count them, but there must be a more elegant solution. What can I do other than looping and counting to find these tables?
Upvotes: 1
Views: 374
Reputation: 92903
Here's a technique that uses the :gt
(greater-than) selector to count rows for you:
$bigtable = $('table').find('tr:gt(5)').closest('table'); // :gt is zero-indexed
After that, you can use .not()
to get all the tables with six rows or less:
$smtable = $('table').not($bigtable);
http://jsfiddle.net/mblase75/44YEf/
Upvotes: 0
Reputation: 33865
I would use the .filter()
method to filter out only the tables that match your criteria. In your case you could select all rows within the table and check the length. Something like this:
// Get tables with more than six rows
$("table").filter(function (){
return $("tr", this).length > 6;
});
Upvotes: 1
Reputation: 587
You're looking for .filter()
$('table').filter(function(index) {
return $('tr', this).length > 6;
});
Upvotes: 0
Reputation: 272106
You can use jQuery.filter
for this:
$("table").filter(function(){
return $(this).find("tr").length > 6;
});
Upvotes: 1