Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Traversing the DOM Child Traversal

Cannot read property '1' of null

What is wrong?

app.js
let section = document.querySelector('section');
let paragraphs = section.children;
for (let i =0 ; i< paragraphs.length ; i+= 1 ){
const panda = section.querySelector('div');
  panda.style.color='blue';
}
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Child Traversal</title>
    </head>
    <body>
        <section>
          <div>
            <p>This is the first paragraph</p>
            <p>This is a slightly longer, second paragraph</p>
            <p>Shorter, last paragraph</p>
          </div>
        </section>
        <footer>
            <p>&copy; 2019</p> 
        </footer>
        <script src="app.js"></script>
    </body>
</html>

2 Answers

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 33,558 Points

Hi Leila. So I took a quick look at your code and saw that you were setting the Div element to 'blue' and when I looked at your HTML you had no Div elements. So I thought hey just change the Div to P in your code and it will work. But when I did that only the first line turned blue. I encourage you to try that and see if you can fix the loop.

If you look at your loop you are looping through the paragraphs correctly but instead of using your index i you are trying to do a queryselect. That is double work because all you need to do is loop through your paragraphs using index i.

So

for (let i =0 ; i< paragraphs.length ; i+= 1 ){
panda.style.color='blue';
}

is almost correct. Just replace panda with the element of the paragraphs array. Good luck Leila!!

Thank you so much, that's worked properly.