$("#mylist"). append("<li>New list item</li>");
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With