{"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 \n
In this paragraph is an important section
\n\n"},"desc":"Retrieve one of the elements matched by the jQuery object.","longdesc":"\nThe .get() method grants us access to the DOM nodes underlying each jQuery object. Consider a simple unordered list:
\n<ul>\n <li id=\"foo\">foo</li>\n <li id=\"bar\">bar</li>\n</ul>\n \n With an index specified, .get( index ) retrieves a single element:
\nconsole.log( $( \"li\" ).get( 0 ) );\n \n Since the index is zero-based, the first list item is returned:
\n\n <li id=\"foo\">\n
\n\nEach jQuery object also masquerades as an array, so we can use the array dereferencing operator to get at the list item instead:
\n\nconsole.log( $( \"li\" )[ 0 ] );\n \n However, this syntax lacks some of the additional capabilities of .get(), such as specifying a negative index:
\n\nconsole.log( $( \"li\" ).get( -1 ) );\n \n A negative index is counted from the end of the matched set, so this example returns the last item in the list:
\n\n <li id=\"bar\">\n
\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 - \n\nConsider a simple unordered list:
\n\n<ul>\n <li id=\"foo\">foo</li>\n <li id=\"bar\">bar</li>\n</ul>\n \n Without a parameter, .get() returns an array of all of the elements:
\nconsole.log( $( \"li\" ).get() );\n \n All of the matched DOM nodes are returned by this call, contained in a standard array:
\n\n [<li id=\"foo\">, <li id=\"bar\">]\n
\n "}]}