Get the number of non-hidden rows in the table

I want to count the number of rows in my table that are not hidden. I can tell if the string is hidden by checking the attribute style tr : <tr style="display: none; "> . How do you calculate this using jquery?

+8
jquery jquery-selectors
source share
5 answers

you can use the: visible selector.

 $('tr:visible').length; 

here is a fiddle demonstrating this:

http://jsfiddle.net/cX6jb/

+13
source share

: The view selector will only display visible elements.

 var count = $('#your-table tr:visible').length; 

jsFiddle Demo

If you already have a variable containing your strings, you can also use the filter method.

 var $rows = $('#your-table tr'), visibleCount = $rows.filter(':visible').length; 
+5
source share
 $('tr').filter(':visible').length 

TA-dah! Note. Visible is a jQuery selector, so it’s much faster to get your elements using a valid css selector, then filter them out.

: visible

+3
source share

There may be an easier way, but you can do it

 var a = $('tr').length; var b = $('tr[style="display:none;"]').length; alert(a - b); 

Example: http://jsfiddle.net/YV3cy/

+1
source share

If you recognize a specific table, do it

 $("#tableid tr:visible").length 
+1
source share

All Articles