1 line
1.8 KiB
JSON
1 line
1.8 KiB
JSON
{"name":"size","type":"method","title":".size()","deprecated":"1.8","removed":null,"desc":"Return the number of elements in the jQuery object.","categories":["miscellaneous/dom-element-methods","version/1.0","deprecated/deprecated-1.8"],"entries":[{"return":"Integer","signatures":{"added":"1.0"},"examples":{"desc":"Count the divs. Click to add more.","code":"\n$( document.body )\n .click(function() {\n $( this ).append( $( \"<div>\" ) );\n var n = $( \"div\" ).size();\n $( \"span\" ).text( \"There are \" + n + \" divs. Click to add more.\" );\n })\n\n // Trigger the click to start\n .click();\n","css":"\n body {\n cursor: pointer;\n min-height: 100px;\n }\n div {\n width: 50px;\n height: 30px;\n margin: 5px;\n float: left;\n background: blue;\n }\n span {\n color: red;\n }\n","html":"\n<span></span>\n<div></div>\n"},"longdesc":"\n <p>The <code>.size()</code> method is deprecated as of jQuery 1.8. Use the <code><a href=\"/length/\">.length</a></code> property instead.</p>\n\n <p>The <code>.size()</code> method is functionally equivalent to the <code><a href=\"/length/\">.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>\n <p>Given a simple unordered list on the page:</p>\n <pre><code>\n<ul>\n <li>foo</li>\n <li>bar</li>\n</ul>\n </code></pre>\n <p>Both <code>.size()</code> and <code>.length</code> identify the number of items:</p>\n <pre><code>\nalert( \"Size: \" + $( \"li\" ).size() );\nalert( \"Size: \" + $( \"li\" ).length );\n </code></pre>\n <p>This results in two alerts:</p>\n <p>\n <samp>Size: 2</samp>\n </p>\n <p>\n <samp>Size: 2</samp>\n </p>\n "}]} |