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

JS and DOM quiz

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

const body = ______________;

My answer const body = document.getElementsByTagName("body");

And it got wrong.

Yes, I had this same issue. There is a similar question in a future quiz, and leaving the semi-colon off allowed it to be excepted as a valid answer. I think the body one is too early to answer using the .querySelector, but it's quite possible it's out of order. This particular course is extremely particular about it's answers, making it extremely annoying.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,254 Points

Is the semi colon not given as part of the answer though? So in this case you don't need to supply it! :-)

I didn't see one on the body question. I did see one on the future question. Oversight?

1 Answer

Steven Parker
Steven Parker
243,656 Points

:point_right: You forgot to convert from a collection to an element.

Since the function returns a collection, but you are being asked to store just the element, you must convert. In this case, since there is only one element of that type in the document, you can convert by simply subscripting with the index 0:

const body = document.getElementsByTagName("body")[0];

The correct answer is scored properly with or without the ending semicolon. Semicolons are optional (but considered a "best practice") in JavaScript when the statement is followed by a newline.

Thanks!

What if I want to select multiple elements?

Leave the index off.

document.getElementsByTagName("body");