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

Changing paragraph text color programmatically

I need to loop through the 'paragraphs' object and, if a child of that object is a paragraph, to change the text color of the paragraph to blue. I'm checking each child to see if it's a paragraph, but I must be missing the appropriate property to set the color to blue. Hoping someone can assist.

const section = document.querySelector('section');
let paragraphs = section.children;
for (let i=0; i < paragraphs.length; i++) {
    if (paragraphs[i] === 'p') {

        paragraphs[i].style.background = 'blue';      
<!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; 2016</p> 
        <script src="app.js"></script>

Never mind, I figured it out!

1 Answer

Steven Parker
Steven Parker
228,991 Points

Good job on resolving your issue! :+1: But for the benefit of other students reading this question:

  • to identify the type of element, you can examine the tagName property
  • values in tagName will always be upper case
  • the name of the property that sets text color is simply "color" (not "background")