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 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 37,777 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.