Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
215,958 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")