Niraj Choubey
Niraj Choubey

Reputation: 4040

What is difference between get() and eq() in jquery?

What is the difference between

var row1 = $('tr').get(0);

and

var row2 = $('tr').eq(0);

Upvotes: 18

Views: 6348

Answers (2)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

get(0) returns the first DOM element matched by the selector.

eq(0) returns a jQuery object containing the first DOM element matched by the selector.

In other words, $("selector").get(0) is equivalent to $("selector").eq(0).get(0).

Upvotes: 20

user113716
user113716

Reputation: 322492

The .get() method returns a DOM element at the given index.

The .eq() method returns a DOM element at the given index, wrapped in a jQuery object.

Another difference is that if you don't pass an argument to .get(), it will return an Array of the DOM elements in the jQuery object.

Upvotes: 9

Related Questions