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

Isaac Hughes
Isaac Hughes
3,711 Points

Cant seem to figure out children background color

The error i get is that it is undefined but I cannot figure out why it does that

app.js
const section = document.querySelector('section');
let paragraphs=section.children;

for(let i=0; i< paragraphs.length; i+=1){
    paragraphs.style.backgroundColor='blue';
}
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Child Traversal</title>
    </head>
    <body>
        <section>
            <p>This is the first paragraph</p>
            <p>This is a slightly longer, second paragraph</p>
            <p>Shorter, last paragraph</p>
        </section>
        <footer>
            <p>&copy; 2016</p> 
        </footer>
        <script src="app.js"></script>
    </body>
</html>
Cheo R
Cheo R
37,150 Points
paragraphs.style.backgroundColor='blue';

You're close. Two things to look at:

  • paragraphs: needs to be indexed
  • backgroundColor: if you look at the preview, it shows p's background as blue, but the prompt asks to turn the paragraph text blue.
Isaac Hughes
Isaac Hughes
3,711 Points

my problem though is trying to index it I looked back at the videos but cant seem to find it

1 Answer

don't forget that index or i is a variable (let i = 0) just like your paragraphs variable

the index allows you to use or move the same code across the paragraphs variable like an array [p, p, p]

it should look something like: paragraphs [].style.....

Isaac Hughes
Isaac Hughes
3,711 Points

I think the main issue I was having was that I was using backgroundColor instead of .color it let me pass then it mentioned something about index but I can't remember what it was, I seriously feel like smacking my forehead on the desk sometimes gahh XD