Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Getting a Handle on the DOM Selection Review

Shawna Carey
Shawna Carey
3,691 Points

Not sure why my answer is incorrect. Is 'body' not the name of the tag?

I know that document.getElementsByTagName returns an HTML collection to the body variable listed. I put 'body' as the tag to grab, but it is saying my answer is incorrect.

2 Answers

You're right, body is a tag name - the key here is the hint on the question, which says that getElementsByTagName returns a collection. There might only be one body, but if you selected another element tag like h1, there could very well be multiple, which is why a collection is returned, even if there's only one match.

It should pass if you add [0] to indicate the item at index 0 - document.getElementsByTagName('body')[0]

Shawna Carey
Shawna Carey
3,691 Points

Thank you for your response!

document.getElementsByTagName('body') returns an array.

document.getElementsByTagName('body')[0] returns the body html.

Play around with it in the Chrome Developer Tools console!