Im counting my li elements with the following jQuery script:
HTML:
<ul class="relatedelements">
<li style="display:none;" class="1">anything</li>
<li style="display:none;" class="2">anything</li>
<li style="display:none;" class="3">anything</li>
</ul>
jQuery:
$(function() {
var numrelated=$('.relatedelements > li').length;
$('.num-relatedelements').html(numrelated);
});
--> The script returns: 3
I change the style="display: none"
property of some of the li elements when $(document).ready
with jQuery, like: $('.2').show();
I now want to change the script in a way to count only the visible li elements with the following script (i just added :visible following the jQuery docs):
$(function() {
var numrelated=$('.relatedelements > li:visible').length;
$('.num-relatedelements').html(numrelated);
});
--> The script returns: nothing
I have no clue why it doesn't work out - maybe anyone has any tip or idea? Any help is much appreaciated. Thanks upfront!
:visible
it crashes. However I'm now using a workaround. When I change the display:none css property I'm adding a class.addClass("countme")
to the li elements that are visible. I'm now counting all li elements that have the classli.countme
Thanks for your help! – Conversant