ale
ale

Reputation: 11830

Get the number of non-hidden rows in a table

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

Upvotes: 8

Views: 7491

Answers (5)

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

If you find out specific table do this

$("#tableid tr:visible").length

Upvotes: 1

Andrew
Andrew

Reputation: 13853

$('tr').filter(':visible').length

Tada! Note: Visible is a jQuery selector, so it is much faster to get your elements using a valid css selector then filtering them.

:visible

Upvotes: 3

Jason Gennaro
Jason Gennaro

Reputation: 34853

There is probably an easier way, but you could do this

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

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

Upvotes: 1

kapa
kapa

Reputation: 78731

The :visible selector will only select the visible items.

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

jsFiddle Demo

If you already have a variable that holds your rows, you can also use the filter method.

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

Upvotes: 5

Patricia
Patricia

Reputation: 7802

you can use the :visible selector.

$('tr:visible').length;

here is a fiddle demonstrating this:

http://jsfiddle.net/cX6jb/

Upvotes: 15

Related Questions