JavaScript JavaScript and the DOM Getting a Handle on the DOM Using CSS Queries to Select Page Elements

Selection Review Quiz Questions:

On the quiz there is a question which asks how to select the body tag using document.getElementsByTagName and to assign it to the variable body.

They give you the start const body then an input field to put your answer. I have tried so many ways but always says wrong answer!

I put in: document.getElementsByTagName('body');

What am i doing wrong?

2 Answers

Steven Parker
Steven Parker
179,882 Points

:point_right: You forgot to apply the index for the "zero" element.

The function returns a collection, so if you want just a single element you have to apply an index. Since there's only one "body" element, the index will be 0.

document.getElementsByTagName('body')[0]
Zachary Harriott
Zachary Harriott
5,630 Points

Thanks! This was driving me crazy.

Elizabeth Chai
Elizabeth Chai
9,692 Points

Just the question I was looking for. Was so confused and tried the quiz a few times...