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 trialDaniel Toma
2,370 PointsChanging 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>
<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>© 2016</p>
</footer>
<script src="app.js"></script>
</body>
</html>
1 Answer
Steven Parker
231,271 PointsGood job on resolving your issue! 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")
Daniel Toma
2,370 PointsDaniel Toma
2,370 PointsNever mind, I figured it out!