Find length of a visible elements using jQuery
Hi all I need to find the length of all the li elements which has display block only. How can this be possible using jQuery. I have a category menu block which has more link at the bottom which when clicked will displays the all categories.The Bottom link now turn to Less which when clicked displays less items. Here is the code.
var list = $('.menu-categories-list ul li:gt(3)');
list.hide();
$('#ClickMore').click(function() {
list.slideToggle(400);
if( $(this).parent().prev().children().length < 1 ) {
$(this).html('Less...');
}
开发者_StackOverflow社区 else {
$(this).html('More...');
}
return false;
});
YOu can have a look at the link. The categories block on the left side
I would suggest:
$('.menu-categories-list ul li:visible').size()
in condition:
if ( $('.menu-categories-list ul li:visible').size() >= 4 ) {
// do something
}
For anyone coming from Google...
The .size()
method has been deprecated as of jQuery 1.8, use .length
instead:
$('.menu-categories-list ul li:visible').length
Will return an integer value depicting the number of visible li
elements that are children of .menu-categories > ul
Written as an answer as I don't have enough rep to comment on Teneff's answer
精彩评论