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 Selection Review

Why is my answer mentioned below to this quiz incorrect? And what is the right answer?

The quiz is to write syntax for getElementsByTagName for selecting the elements with the 'body' tag. I wrote:

const body = document.getElementsByTagName('body');

This came up as incorrect answer. Why is this incorrect? Also, as a general question on Quizes on the site, how do you find out the correct answers to the quizes you get wrong?

1 Answer

andren
andren
28,558 Points

This came up as incorrect answer. Why is this incorrect?

As the question itself mentions the getElementsByTagName method returns a collection, not a single element. And that is the case even if there is only one element that matches your selection criteria. Therefore the code you posted would result in a collection of elements, not the body element itself. This matters since most methods intended for single elements cannot be applied to a collection of elements.

To pull the body element out of the collection you can use bracket notation like this: document.getElementsByTagName('body')[0]. That will return the first item of the collection, which in this case would be the body element.

Also, as a general question on Quizes on the site, how do you find out the correct answers to the quizes you get wrong?

You post a question on this forum or search around on Google. Sadly there isn't any official way to just see the answer to the quiz or challenges in general on Treehouse.

Thank you for your response! I read the question again and they wanted to assign the body variable with the element and not the collection of elements.