Item 2
  • Item 3<...">

    How to count list items that are not hidden?

    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 hidden elements from the general list

     $('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() 

    This does not work. Any ideas?

    +6
    jquery html css
    source share
    2 answers

    The opposite :hidden :visible - jQuery docs .

     $('ul li:visible').size() 
    +22
    source share

    The simplest form:

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

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

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

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

    +2
    source share

    All Articles