How can I count the number of items in div that are hidden 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.
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.
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.
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;
I think that
$("#someElement > *").filter(":hidden").size();
will work.
Updated: Added the '*'. Note that this will select the immediate children of #someElement
.
$("#someElement *:hidden").size()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With