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 trialYeshaya Coffman
Full Stack JavaScript Techdegree Graduate 22,885 PointsHow is this the wrong answer to the question?
How would you select the body element using document.getElementsByTagName, and store it in the variable body?
const body = document.getElementsByTagName('body');
6 Answers
Chris Shaw
26,676 PointsHi Yeshaya Coffman,
When using getElementsByTagName
we have to remember that it returns an HTML Collection rather than a single Element like getElementById
, because of this we need to select the first item in the collection.
const body = document.getElementsByTagName('body')[0];
Your answer was correct except for missing the item selection.
Happy coding!
Ben Herro
Full Stack JavaScript Techdegree Student 8,722 PointsShould the question not be 'How would you select the FIRST body element'? It's worded confusingly.
Markus Mönch
16,383 Pointsbut they dont do that before in the course with the li element
Ben Schnell
14,763 PointsYeah, I had the same question. The quiz should say what's wrong.
jiwan gurung
4,248 Pointsbut why we need to give an index number to the body?body is not an array is it?
Chris Shaw
26,676 PointsAs explained in my original post, the getElementsByTagName
method returns an HTML Collection. Specifying an index allows us to get the first element from the collection.
This isn't specific to the body element, it applies to any selector we use on this method.
Lorence Cano
5,807 Pointsyour answer is correct. I have the same answer and it worked out fine.
Yeshaya Coffman
Full Stack JavaScript Techdegree Graduate 22,885 PointsYeshaya Coffman
Full Stack JavaScript Techdegree Graduate 22,885 PointsBut I don't always have to do it or I?
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsWhen using
getElementsByTagName
you always have to select an item in the collection, an alternate way of accessing the body element is to use the below.const body = document.body;
Hope that helps
Yeshaya Coffman
Full Stack JavaScript Techdegree Graduate 22,885 PointsYeshaya Coffman
Full Stack JavaScript Techdegree Graduate 22,885 Pointsthanks
Robert Hemfelt
7,303 PointsRobert Hemfelt
7,303 PointsIn the example they don't select 1 item from the array, they use:
const myList = document.getElementsByTagName('li');
which I assume selects all <li> elements.
Wouldn't const body = document.getElementsByTagName('body'); just select all body elements (even though there's only 1)?
Chufan Xiao
18,955 PointsChufan Xiao
18,955 Pointsthat's awesome. thx
S Campos
413 PointsS Campos
413 PointsI agree with Ben Herro: 'Should the question not be 'How would you select the FIRST body element'? It's worded confusingly.'
Although, unlike an <li> or <p>, I've never seen more than one <body> this question is not very good.?