.size()


.size()Returns: Integerversion deprecated: 1.8, removed: 3.0

Description: Return the number of elements in the jQuery object.

  • version added: 1.0.size()

    • This method does not accept any arguments.

Note: This method has been removed in jQuery 3.0. Use the .length property instead.

The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.

Given a simple unordered list on the page:

1
2
3
4
<ul>
<li>foo</li>
<li>bar</li>
</ul>

Both .size() and .length identify the number of items:

1
2
alert( "Size: " + $( "li" ).size() );
alert( "Size: " + $( "li" ).length );

This results in two alerts:

Size: 2

Size: 2

Example:

Count the divs.

1
2
3
4
5
6
7
8
9
$( document.body )
.on( "click", function() {
$( this ).append( $( "<div>" ) );
var n = $( "div" ).size();
$( "span" ).text( "There are " + n + " divs. Click to add more." );
} )
// Trigger the click to start
.trigger( "click" );