Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Count number of list elements?

Tags:

html

jquery

People also ask

How to Count list items in jQuery?

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

What is .each in jQuery?

The each() method in jQuery specifies a function that runs for every matched element. It is one of the widely used traversing methods in JQuery. Using this method, we can iterate over the DOM elements of the jQuery object and can execute a function for every matched element.

What is jQuery length?

The length property in jQuery is used to count the number of elements in the jQuery object. The size() function also returns the number of elements in the jQuery object.


Try:

$("#mylist li").length

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

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

?


var listItems = $("#myList").children();

var count = listItems.length;

Of 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.


You have the same result when calling .size() method or .length property but the .length property is preferred because it doesn't have the overhead of a function call. So the best way:

$("#mylist li").length

I think this should do it:

var ct = $('#mylist').children().size();