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 JavaScript and the DOM (Retiring) Getting a Handle on the DOM Using CSS Queries to Select Page Elements

Selection Review Quiz Questions:

On the quiz there is a question which asks how to select the body tag using document.getElementsByTagName and to assign it to the variable body.

They give you the start const body then an input field to put your answer. I have tried so many ways but always says wrong answer!

I put in: document.getElementsByTagName('body');

What am i doing wrong?

2 Answers

Steven Parker
Steven Parker
229,744 Points

:point_right: You forgot to apply the index for the "zero" element.

The function returns a collection, so if you want just a single element you have to apply an index. Since there's only one "body" element, the index will be 0.

document.getElementsByTagName('body')[0]
Zachary Harriott
Zachary Harriott
5,630 Points

Thanks! This was driving me crazy.

Elizabeth Chai
Elizabeth Chai
9,692 Points

Just the question I was looking for. Was so confused and tried the quiz a few times...