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 trialLeila Mahmoudi
5,037 PointsCannot 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');
panda.style.color='blue';
}
<!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>© 2019</p>
</footer>
<script src="app.js"></script>
</body>
</html>
2 Answers
Mark Sebeck
Treehouse Moderator 38,297 PointsHi 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!!
Leila Mahmoudi
5,037 PointsThank you so much, that's worked properly.
Mark Sebeck
Treehouse Moderator 38,297 PointsYouβre welcome Leila. Happy to help!