Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery : eq() vs get()

I'm new to jQuery, and I'm wondering what the difference is between jQuery's get() and eq() functions. I may misunderstand what the get() function does, but I thought it odd that I couldn't call a function on the returned on the returned element in the same line.

//Doesn't work I.e.  $("h2").get(0).fadeIn("slow");  //Works $("h2").eq(0).fadeIn("slow"); 
like image 429
contactmatt Avatar asked Jan 17 '11 03:01

contactmatt


People also ask

What does EQ stand for jQuery?

jQuery eq() Method The eq() method returns an element with a specific index number of the selected elements. The index numbers start at 0, so the first element will have the index number 0 (not 1).

What is $$ in jQuery?

$ sign is just a valid javascript identifier which is used as an alias for jQuery. Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it.


1 Answers

.get() and .eq() both return a single "element" from a jQuery object array, but they return the single element in different forms.

.eq() returns it as a jQuery object, meaning the DOM element is wrapped in the jQuery wrapper, which means that it accepts jQuery functions.

.get() returns an array of raw DOM elements. You may manipulate each of them by accessing its attributes and invoking its functions as you would on a raw DOM element. But it loses its identity as a jQuery-wrapped object, so a jQuery function like .fadeIn won't work.

like image 94
Steven Avatar answered Oct 12 '22 20:10

Steven