Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 261
sprugman Avatar asked May 28 '10 19:05

sprugman


People also ask

How do I get the number of rows in JavaScript?

To get the row count of an HTML table with JavaScript, we can use the rows. length property to get the total number of rows in the table. And to get the number of rows in tbody , we can use the tBodies[0]. rows.

How do I count the number of elements in jQuery?

To count all HTML elements, we use length property. The length property is used to count number of the elements of the jQuery object. where selector is the object whose length is to be calculated.

How do I get the first row of a table in jQuery?

You could also have table > thead > tr and then table > tbody > tr. So you might need to specify whether you want the first row from the thead or the tbody. find('tr:first') will select the tr in the thead.


4 Answers

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;
like image 97
Nick Craver Avatar answered Oct 04 '22 16:10

Nick Craver


$('tr:visible').length

like image 43
Tatu Ulmanen Avatar answered Oct 04 '22 14:10

Tatu Ulmanen


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.

like image 31
Kailas Avatar answered Oct 04 '22 15:10

Kailas


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

like image 36
Brian Mains Avatar answered Oct 04 '22 16:10

Brian Mains