Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Abir Ahmed
PLUS
Abir Ahmed
Courses Plus Student 8,616 Points

How do I select a plain old JavaScript representation of a DOM element?

Why answer is $("element")[0] only, why not this answer is incorrect $("element")[1] in the quiz situated in Front-end-web development < Jquery Basics < Creating a simple drawing application < 5th if you count from last.

This is my main question that both are correct (in my opinion), can you please explain if I am wrong.

1 Answer

Adam Pengh
Adam Pengh
29,881 Points

$("element")[0] is correct because it is selecting the first index of the element array since JavaScript indexes start at 0. $("element")[1] would try to select the 2nd index of the element array.

Abir Ahmed
Abir Ahmed
Courses Plus Student 8,616 Points

Yes I know that, but both the answer is true, if question was somewhat like: Which is indicating the selection of first element in DOM tree then the answer would be satisfactory. This question really doesn't make any sense to me.