tags:

views:

2548

answers:

3

I've got a list that is generated from some server side code, before adding extra stuff to it with jQuery I need to figure out how many items are already in it.

<ul id="mylist">
    <li>Element 1</li>
    <li>Element 2</li>
</ul>

Any ideas?

+3  A: 

Try:

$("#mylist li").size()

Just curious: why do you need to know the size? Can you just use:

$("#mylist").append("<li>New list item</li>");

?

cletus
I'm using appending to add the new ones, but I've got a limit on the number of items that can be in the list, in this state the user can still add upto a max of 4 items, but 2 might have come from the previous state. :)
Tom
+1  A: 

I think this should do it:

var ct = $('#mylist').children().size();
Andrew Barrett
+1  A: 
var listItems = $("#myList").children();

var count = listItems.length;

Off course you can condense this with

var count = $("#myList").children().length;

For more help with jQuery http://docs.jquery.com/Main_Page is a good place to start.

Nick Allen - Tungle139