Thursday, April 21, 2011

Jquery count number of hidden elements within div

How can I count the number of items in div that are hidden?

Thanks in advance

From stackoverflow
  • I think that

    $("#someElement > *").filter(":hidden").size();
    

    will work.

    Updated: Added the '*'. Note that this will select the immediate children of #someElement.

    Zed : Nope, this will return 1, if #someElement is hidden, 0 otherwise.
    jscharf : You're right - the selector is missing a relation. Updated.
    Ronal : Excellent answer, thank you!
  • Direct children of someElement that are hidden:

    $('#someElement > :hidden').length;
    

    Any descendants of someElement that are hidden:

    $('#someElement :hidden').length;
    

    If you already have a jQuery object you can use it as the context:

    var ele = $('#someElement');
    
    $(':hidden', ele).length;
    
  • $("#someElement *:hidden").size()
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.