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

Stuck :(

Challenge Task 1 of 2

On line 2 of app.js, get all the paragraph elements within section and assign them to the paragraphs variable.

I tried answering:

const section = document.querySelector('section'); let paragraphs = document.getElementsByTagName('p');

I figure the correct answer is very easy and I'm going to kick myself. Please help. Thanks!

Which challenge is this for? From the question, it's probably just as simple as:

let paragraphs = section.getElementsByTagName('p');

Since it's only asking for paragraph tags within the section element, you want to call the getElementsByTagName on the element, and not the entire document.

4 Answers

Hi Brian, my comment was wrong, as you've probably noticed.

let paragraphs = section.children;

This is just about using an elements children property.

Thanks very much!

If you do section.children, wouldn't that select other html elements too?

Hi, for this same question I tried 'section.querySelectorAll('p');' Why wouldn't this work?

In this task we had luck that all childrens was paragraphs:

<section> <p>This is the first paragraph</p> <p>This is a slightly longer, second paragraph</p> <p>Shorter, last paragraph</p> </section>

so we might use section.children;

But what if there was not only paragraphs in section tag? how could we fetch only 'p' tag? because getElementsByTagName did not worked...

<code> let paragraphs = section.children; </code> Is the answer but actually this is not correct as this would select all HTML elements not just the paragraphs.