64

I've got this html:

<table>
    <tr style="display:table-row"><td>blah</td></tr>
    <tr style="display:none"><td>blah</td></tr>
    <tr style="display:none"><td>blah</td></tr>
    <tr style="display:table-row"><td>blah</td></tr>
    <tr style="display:table-row"><td>blah</td></tr>
</table>

I need to count the number of rows that don't have display:none. How can I do that?

sprugman
  • 19,351
  • 35
  • 110
  • 163

4 Answers4

142

You can use the :visible selector and .length like this:

var numOfVisibleRows = $('tr:visible').length;

If the <table> itself isn't visible on the screen (:visible returns false if any parent is hidden, the element doesn't have to be hidden directly), then use .filter(), like this:

var numOfVisibleRows = $('tr').filter(function() {
  return $(this).css('display') !== 'none';
}).length;
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
16

$('tr:visible').length

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
9

You can also view particular table visible rows

 var totalRow =  $('#tableID tr:visible').length;
 var totalRowWithoutHeader = totalRow-1;

The totalRowWithoutHeader gives the total row count excluding header row.

Kailas
  • 3,173
  • 5
  • 42
  • 52
3

$("tr:visible") gets you the results of the visible rows, and I think you can then do .length

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • 3
    Not sure why this is upvoted, `.is(":visible")` returns a **boolean**, you cannot call `.length`, instead of `.is()` you'd need `.filter()`. – Nick Craver May 28 '10 at 19:30