views:

1657

answers:

7

I am trying to understand why you would use jQuery.get() and jQuery.get(index). The docs say it is to convert the jQuery selection to the raw DOM object instead of working with the selection as a jQuery object and the methods available to it.

So a quick example:

$("div").get(0).innerHTML;

is the same as:

$("div").html();

Obviously this is a bad example but I am struggling to figure when you would use .get(). Can you help me understand when I would use this method in my code?

+3  A: 

This may come in handy when doing something like (psuedocode):

a = $("div").get()
ajaxsend(a)

There are times when you may need to pass the actual DOM object to other functions, which may not be able to handle the jQuery object.

InsDel
+4  A: 

It happens often when you want to call a native JS method on an element.

//submitting a form...
$('#mySearchForm').get().submit();
scunliffe
You could also force click the submit button $("input#submit").click(); to do the same.
RedWolves
Hmm...I was having a brain fart this morning too. There is also a jQuery method that will trigger a submit. $('#mySearchForm').submit(); will also work. I don't consider your answer a practacle use of get().
RedWolves
A: 

Another possible use of get() is when you are working with XML trees in jQuery.

cbeer
Can you provide a simple code example?
RedWolves
Just something silly like:var xml = $('<dc xmlns="#"><title>Title</title></xml>');var title = $('title', xml).get(0);I don't have any practical uses for it, but I can imagine more circumstances where you want the underlying DOM outside of HTML-land (say, tossing the tree into an XPath matcher or something...)
cbeer
+4  A: 

Sometimes you're using jQuery in noConflict mode and you want to do something like:

var element = jQuery('#jquery_selectors .are > .awesome[and=fast]').get(0);

And then do something in prototype:

$(element).whatever();
thenduks
I think this is the best practical use case so far. I am still hoping for a use case that isn't dependent on another library.
RedWolves
I think you'll find that `get` is just there for completeness sake. Eg, you can turn a DOM element into a jQuery object with el = $(document.getElementById('something')) therefore you should be able to turn a jQuery object into a DOM element with el.get().
thenduks
+2  A: 

This is useful for accessing any JavaScript method that isn't exposed through jQuery. For example, before jQuery supported offset(), I would typically use get() like this:

var offTop = $(element).get(0).offsetTop;

For another example, I used this recently to determine the scrollHeight of an element.

var scrollHeight = $(element).get(0).scrollHeight;

This can also be written as:

var scrollHeight = $(element)[0].scrollHeight;
ern
This is great, is there a list of DOM properties/methods that aren't yet supported in jQuery that might be of use with the get() method? That would be good to know.
RedWolves
I asked a follow question and now I am not so sure your answer is so practical http://stackoverflow.com/questions/770475/dom-properties-methods-that-arent-available-in-jquery/770719#770719
RedWolves
+1  A: 

Cody Lindley (jQuery Team Member) has a great example of why you would use get()

If you ever need to cache a set of elements, because you are about to remove them, the jQuery get() method is really handy. For example, in the code below I am saving all my <li> elements on the page in an array, removing them, and then adding them back into the page using this array. Make sense?

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"&gt;&lt;/script&gt; 
</head>

<body>

<ul> 
    <li>test</li> 
    <li>test</li> 
    <li>test</li> 
    <li>test</li> 
    <li>test</li> 
    <li>test</li> 
    <li>test</li> 
    <li>test</li> 
 </ul>

<script>

var test = $('ul li').get();

$('ul').empty();

$.each(test, function(){ $('ul').append('<li>'+$(this).html() + ' new</li>'); });

</script> 
</body> 
</html>
RedWolves
A: 

I use it to get id for single element

 $(selector).get(0).id
Vladekk