jQuery - Get element from array as jQuery element? jQuery - Get element from array as jQuery element? javascript javascript

jQuery - Get element from array as jQuery element?


Use the eq() method:

$(".myClass").eq(0)

This returns a jQuery object, whereas .get() returns a DOM element.

.eq() lets you specify the index, but if you just want the first you can use .first(), or if you just want the last you can use (surprise!) .last().

"I get an array of elements."

No you don't, you get a jQuery object which is an array-like object, not an actual array.

If you plan to use jQuery much I suggest spending half an hour browsing through the list of all methods.