Count <li> s in <ul> with jQuery
I have a simple list:
<ul id="cyclelist"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> and I use jQuery to cycle through the list. The first problem is finding if the list has more than one item to start with. I expect this to work:
var $list = $('#cyclelist'); if ($list.length > 1) { ... } But length always returns 1. Is there a better way to find the length of a list using jQuery? Or I'm wrong?
+4
4 answers
$('#cyclelist') uses a CSS selector for an element with id = "list of loops" and, therefore, returns a link to a single element / object to correctly; -)
I think you probably meant
$('#cyclelist li') to return list items to a named item.
Another jQuery-esque way to iterate over item collections is to use each, cf http://docs.jquery.com/Core/each
+1