tags:

views:

59

answers:

2

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?

+6  A: 

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

$('ul li:visible').size()
Chad Birch
Oh jeez, thanks. Now I feel silly.It was just very difficult to phrase my google search to find that, I suppose.
Baloneysammitch
If you start on the :hidden page of the docs (http://api.jquery.com/hidden-selector/) and click on "Visibility Filter" in the categories in the top right you'll find it. The jQuery docs are very good.
Chad Birch
+1  A: 

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.

cletus
Thanks for the help with :not()I didn't know you could put those sorts of conditions inside of quotes.
Baloneysammitch