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!

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 JavaScript and the DOM (Retiring) Getting a Handle on the DOM Selection Review

travis halarewich
travis halarewich
9,165 Points

i do not understand what this question is asking, i do not see how my answer is wrong

How would you select the body element using document.getElementsByTagName, and store it in the variable body?

(Hint: Don't forget that this method returns a zero-based collection, not a single element.)

const body = _______ [_];

above is what you are given for the fill in the blank and below is my answer

const body = document.getElementsByTagName["body"];

all i get back is that what i have said is wrong and no feed back as to what is right

Jacob Dahlberg
Jacob Dahlberg
Python Development Techdegree Student 13,154 Points

The value of this quiz question is lost on me as well. Due to the wording, I haven't been able to solve it reading back through fairly meticulous notes. It's referring to a collection (similar to an array).

I know that getElementsByTagName is a method and would fit into "returns a zero-based collection" The answers are always simple, all of my notes point to = document.getElementsByTagName[0];

The question does say "using document.getElementsByTagName" implying that's part of the answer. I moved on and assume it is in the wording of the question and it's okay to not understand everything.

2 Answers

document.getElementsByTagName('body') gets a collection of body Elements (there is of course only one body element). Therefore it is the first element in the collection.

The solution is: document.getElementsByTagName('body')[0];

See the hint provided: Don't forget that this method returns a zero-based collection, not a single element. You will have to specify the first (even though it is the only) element in the collection by using its index [0].