Misha Moroshko
Misha Moroshko

Reputation: 171351

jQuery: How to count the number of elements which "display" isn't "none"?

I use show() and hide() to show and hide rows in a table.

How could I count the number of non-hidden rows (more accurately, rows with display != none) ?

Note that:

$('tr:visible').length

won't work because if the table itself has display=none, the result will always be 0.

Upvotes: 12

Views: 27929

Answers (4)

David Weisser
David Weisser

Reputation: 326

Adding this to the mix. I found this to be a more reliable option.

function recount () {
    var numOfVisibleRows = jQuery('tr.itemtext:visible').length;
    document.getElementById("item_table_count").innerHTML = numOfVisibleRows;
}

I like this because my table itemtext is hiding rows in different ways. I hope it's useful.

See this question for more information: jquery selector to count the number of visible table rows?

Upvotes: 1

Alnitak
Alnitak

Reputation: 339816

Filter your rows based on their actual CSS property:

$('tr').filter(function() {
    return $(this).css('display') !== 'none';
}).length;

Upvotes: 19

miqbal
miqbal

Reputation: 2227

jquery selector to count the number of visible table rows?

Change !== to ===

Upvotes: 2

Richard Dalton
Richard Dalton

Reputation: 35793

Try this:

$('tr:not([style*="display: none"])').length

Example http://jsfiddle.net/infernalbadger/7LvD5/

Upvotes: 22

Related Questions