Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count list elements that are not hidden?

Tags:

html

jquery

css

Starting with a simple list:

<ul>
    <li>Item 1</li>
    <li style="display: none;">Item 2</li>
    <li>Item 3</li>
</ul>

I know that I can subtract the hidden elements from the list total

$('ul li').size() - $('ul li:hidden').size()

But I thought there might be a more elegant way to achieve this with jquery:

$('ul li:hidden:not').size()

That doesn't work. Any ideas?

like image 985
Baloneysammitch Avatar asked Apr 05 '10 23:04

Baloneysammitch


2 Answers

The opposite of :hidden is :visible - jQuery docs.

$('ul li:visible').size()
like image 110
Chad Birch Avatar answered Oct 12 '22 23:10

Chad Birch


The simplest form is:

var hidden = $("ul > li:hidden").length;

On a side note, to correctly use :not():

var hidden = $("ul > li:not(:visible)").length;

Lastly a jQuery object supports the size() method and the length property, which are interchangeable.

like image 35
cletus Avatar answered Oct 13 '22 01:10

cletus