I want to know about .get() method in jquery. I just read the blog on http://api.jquery.com/get/ about the .get() method. I am using the same html but it is not giving the result as shown in document. Can anyone explain .get() in a practical way. What, when, and how to use it.
<ul>
<li id="foo">foo</li>
<li id="bar">bar</li>
</ul>
<script>
$(function(){alert($("li").get())})
</script>
The
.get()method is used to return the native DOM elements in the collection. You can optionally pass an index toget, and it’ll return only that element.This is useful sometimes when you want to mix jQuery with the native DOM API. You might use jQuery to traverse the DOM, but then call
geton it so that you can call the native functions on it.Here’s a fiddle demonstrating that, by using the native
scrollIntoViewmethod.If you want to get a single element from the collection, you can also use regular array-like bracket notation:
P.S. Do not confuse this with the
.eq()method, which will also return the element at the specified index, but wrapped in jQuery.