Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
53 lines (53 sloc) 1.65 KB
<?xml version="1.0"?>
<entry type="method" name="size" return="Number">
<desc>Return the number of elements in the jQuery object.</desc>
<p>The <code>.size()</code> method is functionally equivalent to the <code><a href="">.length</a></code> property; however, <strong>the <code>.length</code> property is preferred</strong> because it does not have the overhead of a function call.</p>
<p>Given a simple unordered list on the page:</p>
<p>Both <code>.size()</code> and <code>.length</code> identify the number of items:</p>
<pre><code>alert( "Size: " + $("li").size() );
alert( "Size: " + $("li").length );</code></pre>
<p>This results in two alerts:</p>
<span class="output">Size: 2</span>
<span class="output">Size: 2</span>
<desc>Count the divs. Click to add more.</desc>
.click(function() {
$(this).append( $("<div>") );
var n = $("div").size();
$("span").text("There are " + n + " divs. Click to add more.");
// trigger the click to start
body { cursor:pointer; min-height: 100px; }
div { width:50px; height:30px; margin:5px;
float:left; background:blue; }
span { color:red; }
<category slug="miscellaneous/dom-element-methods"/>
<category slug="version/1.0"/>
Jump to Line
Something went wrong with that request. Please try again.