Reputation: 2534
How can I use JavaScript to determine if a UL contains 1 or more LI's within?
Pseudo code
if('ul#items' has >= 1 LI){
Thanks,
Upvotes: 11
Views: 34668
Reputation: 16252
With jQuery:
$('ul#items li').length >= 1
Without jQuery:
document.getElementById('items').getElementsByTagName('li').length >= 1
Upvotes: 39
Reputation: 382
If you're using jQuery
if($('ul > li').size()>0) {
}
That will check that the UL element has more than 0 direct child li elements.
Upvotes: 1
Reputation: 665584
Use document.getElementById("items").childNodes.length
and a number comparison operator. If your ul
does contain other nodes than li
, you will have to filter them.
In jQuery:
$("#items").children("li").length
I guess you only want direct children, so don't use find().
Upvotes: 4