tags:

views:

26

answers:

1

I have a list

<ul>
  <li>
  <li>
  <li>
  ...
</ul>

I need jQuery to count the number of items in my list.

thanks

+4  A: 

You can use .length, like this:

var count = $("ul li").length;

.length tells how many matches the selector found, so this counts how many <li> under <ul> elements you have...if there are sub-children, use "ul > li" instead to get only direct children. If you have other <ul> elements in your page, just change the selector to match only his one, for example if it has an ID you'd use "#myListID > li".

Nick Craver