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!

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

quiz question 5 of 5

how do you use documents.getElementsByTagName

2 Answers

Hi malcolm mowlam,

document.getElementsByTagName is a method that returns a collection of elements. For instance, if you were to write something like:

const paragraphs = document.getElementsByTagName('p');
// the above would store a collection of paragraph elements in paragraphs i.e. [p1, p2, p3, p4...]

To then be able to manipulate one of the paragraph elements, you would use indexing. So say you wanted to manipulate the second paragraph in the paragraphs collection (and as such, the second paragraph in the html document), to select it you would write the following: paragraphs[1].

Does that help?

Thank you Brandon helps a great deal