Currently, I am utilizing a jQuery script to count the number of li elements in my HTML code. Here is an example of the setup:
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 current output of this script is: 3
Sometimes, I modify the style="display: none"
property of certain li elements using jQuery within $(document).ready
. For instance, I may execute $('.2').show();
.
Now, I am looking to adjust the script so that it only counts the visible li elements by adding :visible as specified in the jQuery documentation:
$(function() {
var numrelated=$('.relatedelements > li:visible').length;
$('.num-relatedelements').html(numrelated);
});
The updated script currently outputs: nothing
I am uncertain why this modification isn't producing the desired result. If anyone has any suggestions or insights, I would greatly appreciate the assistance. Thank you in advance!