Sunday, May 13, 2012

jquery selector to count the number of visible table rows?


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?


Source: Tips4all

3 comments:

  1. 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;

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

    ReplyDelete