top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between eq() and get() methods in jQuery?

+3 votes
883 views
What is the difference between eq() and get() methods in jQuery?
posted Jul 10, 2015 by Shivaranjini

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it.

get() return a DOM element. The method retrieve the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can't be used.

.eq(n) retrieves the n-1th jQuery object.
.get(n) retrieves the n-1th DOM element. It'd be like doing .eq(n)[0].
:nth-child() is for more complex selectors like :nth-child(2n+1).

You could use it in place of :eq() in selector strings, but I tend to stay away from it and use .eq() to make the selector more readable:

$('ul.parent > li.child:nth-child(2)')
$('ul.parent > li.child:eq(2)')
$('ul.parent > li.child').eq(2)
answer Jul 10, 2015 by Amit Kumar Pandey
...