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
Count number of list elements
alert($("#mylist > li").length);
try
$("#mylist").children().length
and of course the following:
var count = $("#myList").children().length;
can be condensed down to: (by removing the 'var' which is not necessary to set a variable)
count = $("#myList").children().length;
however this is cleaner:
count = $("#mylist li").size();
Another approach to count number of list elements:
var num = $("#mylist").find("li").length;_x000D_
console.log(num);
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<ul id="mylist">_x000D_
<li>Element 1</li>_x000D_
<li>Element 2</li>_x000D_
<li>Element 3</li>_x000D_
<li>Element 4</li>_x000D_
<li>Element 5</li>_x000D_
</ul>
_x000D_
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.
$("button").click(function(){_x000D_
alert($("li").length);_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
<head>_x000D_
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>_x000D_
<meta charset="utf-8">_x000D_
<title>Count the number of specific elements</title>_x000D_
</head>_x000D_
<body>_x000D_
<ul>_x000D_
<li>List - 1</li>_x000D_
<li>List - 2</li>_x000D_
<li>List - 3</li>_x000D_
</ul>_x000D_
<button>Display the number of li elements</button>_x000D_
</body>_x000D_
</html>
_x000D_
I think this should do it:
var ct = $('#mylist').children().size();
Source: Stackoverflow.com