.size()

.size() Returns: Number

Description: Return the number of DOM elements matched by the jQuery object.

  • version added: 1.0.size()

<ul>
  <li>foo</li>
  <li>bar</li>
</ul>

We can determine the number of list items by calling .size():

alert('Size: ' + $('li').size());

This will alert the count of items:

Size: 2

The .length property is a slightly faster way to get this information.

Example:

Count the divs. Click to add more.

<!DOCTYPE html>
<html>
<head>
  <style>body { cursor:pointer; }
 div { width:50px; height:30px; margin:5px; float:left; background:blue; }
 span { color:red; }
 </style>
  <script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<span></span>

 <div></div>
<script>$(document.body).click(function () { $(document.body).append($("<div>"));
var n = $("div").size();
$("span").text("There are " + n + " divs." + "Click to add more.");}).click(); // trigger the click to start</script>
</body>
</html>

Demo: