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

What about const body = document.getElementsByTagName('body'); is incorrect as a method to retrieve and store <body>?

This is in reference to quiz question 1 for Javascript and the DOM after the section on retrieving HTML elements by various methods

I have the same question. It seems to me that this would yield the same thing as: document.getElementsByTagName('body')[0];
since there is only one <body> element

so why aren't they both correct answers? (It would only accept the latter).

1 Answer

.getElementsByTagName() method returns a live HTMLCollection of elements. So if you want to get the <body> you must specify its position in the collection/array like: document.getElementsByTagName(‘body’)[0] or use another method, document.querySelector(‘body’)