Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 636
Ronal Avatar asked Aug 18 '09 19:08

Ronal


People also ask

How to get number of elements in a div using jQuery?

Answer: Use the jQuery . length property You can simply use the jQuery . length property to find the number of elements in a DIV element or any other element. The following example will alert the number of paragraphs in a <div> element having the class . content on document ready event.

How to count elements in jQuery?

To count all HTML elements, we use length property. The length property is used to count number of the elements of the jQuery object. where selector is the object whose length is to be calculated.

Can a div be hidden?

The hidden attribute hides the <div> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <div> element is not visible, but it maintains its position on the page.


3 Answers

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;
like image 129
Keith Avatar answered Oct 06 '22 01:10

Keith


I think that

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

will work.

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

like image 33
jscharf Avatar answered Oct 05 '22 23:10

jscharf


$("#someElement *:hidden").size()
like image 26
Chetan S Avatar answered Oct 05 '22 23:10

Chetan S