control-freak-ide/Control-Freak-Documentation/jQuery/docs/entries/get.json
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00

1 line
3.3 KiB
JSON

{"name":"get","type":"method","title":".get()","deprecated":null,"removed":null,"desc":"Retrieve the DOM elements matched by the jQuery object.","categories":["miscellaneous/dom-element-methods","version/1.0"],"entries":[{"return":"Element","signatures":{"added":"1.0","argument":{"desc":"A zero-based integer indicating which element to retrieve.","name":"index","type":"Integer"}},"examples":{"desc":"Display the tag name of the click element.","code":"\n$( \"*\", document.body ).click(function( event ) {\n event.stopPropagation();\n var domElement = $( this ).get( 0 );\n $( \"span:first\" ).text( \"Clicked on - \" + domElement.nodeName );\n});\n","css":"\n span {\n color: red;\n }\n div {\n background: yellow;\n }\n","html":"\n<span>&nbsp;</span>\n<p>In this paragraph is an <span>important</span> section</p>\n<div><input type=\"text\"></div>\n"},"desc":"Retrieve one of the elements matched by the jQuery object.","longdesc":"\n <p>The <code>.get()</code> method grants us access to the DOM nodes underlying each jQuery object. Consider a simple unordered list:</p>\n <pre><code>\n&lt;ul&gt;\n &lt;li id=\"foo\"&gt;foo&lt;/li&gt;\n &lt;li id=\"bar\"&gt;bar&lt;/li&gt;\n&lt;/ul&gt;\n </code></pre>\n <p>With an index specified, <code>.get( index )</code> retrieves a single element:</p>\n <pre><code>\nconsole.log( $( \"li\" ).get( 0 ) );\n </code></pre>\n <p>Since the index is zero-based, the first list item is returned:</p>\n <p>\n <samp>&lt;li id=\"foo\"&gt;</samp>\n </p>\n\n <p>Each jQuery object also masquerades as an array, so we can use the array dereferencing operator to get at the list item instead:</p>\n <pre><code>\nconsole.log( $( \"li\" )[ 0 ] );\n </code></pre>\n <p>However, this syntax lacks some of the additional capabilities of .get(), such as specifying a negative index:</p>\n <pre><code>\nconsole.log( $( \"li\" ).get( -1 ) );\n </code></pre>\n <p>A negative index is counted from the end of the matched set, so this example returns the last item in the list:</p>\n <p>\n <samp>&lt;li id=\"bar\"&gt;</samp>\n </p>\n "},{"return":"Array","signatures":{"added":"1.0"},"examples":{"desc":"Select all divs in the document and return the DOM Elements as an Array; then use the built-in reverse() method to reverse that array.","code":"\nfunction display( divs ) {\n var a = [];\n for ( var i = 0; i < divs.length; i++ ) {\n a.push( divs[ i ].innerHTML );\n }\n $( \"span\" ).text( a.join(\" \") );\n}\ndisplay( $( \"div\" ).get().reverse() );\n","css":"\n span {\n color: red;\n }\n","html":"\nReversed - <span></span>\n\n<div>One</div>\n<div>Two</div>\n<div>Three</div>\n"},"desc":"Retrieve the elements matched by the jQuery object.","longdesc":"\n <p>Consider a simple unordered list:</p>\n <pre><code>\n&lt;ul&gt;\n &lt;li id=\"foo\"&gt;foo&lt;/li&gt;\n &lt;li id=\"bar\"&gt;bar&lt;/li&gt;\n&lt;/ul&gt;\n </code></pre>\n <p>Without a parameter, <code>.get()</code> returns an array of all of the elements:</p>\n <pre><code>\nconsole.log( $( \"li\" ).get() );\n </code></pre>\n <p>All of the matched DOM nodes are returned by this call, contained in a standard array:</p>\n <p>\n <span class=\"result\">[&lt;li id=\"foo\"&gt;, &lt;li id=\"bar\"&gt;]</span>\n </p>\n "}]}