How do I get first element rather than using [0] in jQuery?

You can use .get(0) as well but…you shouldn’t need to do that with an element found by ID, that should always be unique. I’m hoping this is just an oversight in the example…if this is the case on your actual page, you’ll need to fix it so your IDs are unique, and use a class (or another attribute) instead.

.get() (like [0]) gets the DOM element, if you want a jQuery object use .eq(0) or .first() instead 🙂

Leave a Comment