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) Traversing the DOM Child Traversal

Cannot read property '1' of null

What is wrong?

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

2 Answers

Mark Sebeck
Mark Sebeck
Treehouse Moderator 36,463 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.


for (let i =0 ; i< paragraphs.length ; i+= 1 ){

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

Thank you so much, that's worked properly.