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 trialFernando Jimenez
Courses Plus Student 8,455 PointsHow come this is the wrong answer for this question?
How would you select the body element using document.getElementsByTagName, and store it in the variable body?
(Hint: Don't forget that this method returns a collection, not a single element.)
const body = document.getElementsByTagName("body");
Why is document.getElementsByTagName("body"); wrong?
1 Answer
Steven Parker
231,236 PointsWell, that hint about returning a collection is the clue. The instructions ask for an element, but the function returns a collection.
Since an HTMLCollection is like an array in many ways, it can be indexed. So an easy way to get a single element from a collection is to apply an index to it. And since a document only has one "body", you know the collection's only element will be at index 0.