If a ul
has more than one li
-element inside of it, something should happen, otherwise not!
What am I doing wrong?
if ( $('#menu ul').length > 1 ) {
You have to count the li elements not the ul elements:
if ( $('#menu ul li').length > 1 ) {
If you need every UL element containing at least two LI elements, use the filter function:
$('#menu ul').filter(function(){ return $(this).children("li").length > 1 })
You can also use that in your condition:
if ( $('#menu ul').filter(function(){ return $(this).children("li").length > 1 }).length) {
The correct syntax is
$('ul#menu li').length
alert( "Size: " + $( "li" ).size() );
alert( "Size: " + $( "li" ).length );
Both .size() and .length return number of item. but size() method is deprecated (JQ 1.8). .length property can use for instead of size().
also you can use
alert($("ul").children().length);
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