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 trialthomasaurelius
3,142 Pointshttps://teamtreehouse.com/library/javascript-and-the-dom-2/getting-a-handle-on-the-dom/selecting-multiple-elements
Can someone tell me why this is not accepting an answer that is providing a working solution?
My solution works, all 7 li items get their color applied.
var listItems = document.querySelector('#rainbow');
var colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];
for(var i = 0; i < colors.length; i ++) {
listItems.children[i].style.color = colors[i];
}
<!DOCTYPE html>
<html>
<head>
<title>Rainbow!</title>
</head>
<body>
<ul id="rainbow">
<li>This should be red</li>
<li>This should be orange</li>
<li>This should be yellow</li>
<li>This should be green</li>
<li>This should be blue</li>
<li>This should be indigo</li>
<li>This should be violet</li>
</ul>
<script src="js/app.js"></script>
</body>
</html>
1 Answer
Cameron Childres
11,820 PointsThe question states:
Complete the code by setting the variable listItems
to refer to a collection.
You provided the collection by using the .children property on listItems -- you didn't actually set the variable listItems to refer to the collection.
Working solution:
var listItems = document.getElementById("rainbow").children;
var colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];
for(var i = 0; i < colors.length; i ++) {
listItems[i].style.color = colors[i];
}
thomasaurelius
3,142 Pointsthomasaurelius
3,142 PointsThank you. The preceding instructions don't relay that bit very well.
I tried document.querySelector('[id=rainbow]').children but didn't try it it with getElementById.
Thank you!
Cameron Childres
11,820 PointsCameron Childres
11,820 PointsNo worries. I find myself running in to the same issue a lot -- solving for the outcome but skipping over some specific parameter in the question. I probably spend more time re-reading the questions than I do writing the code!
Edit -- I just caught up to you on this challenge, document.querySelector('[id=rainbow]').children works for me:
Kwame Afrifa Obeng -Ofori
2,238 PointsKwame Afrifa Obeng -Ofori
2,238 Pointscan you please explain the .children part
Cameron Childres
11,820 PointsCameron Childres
11,820 PointsSure thing Kwame,
If you haven't already then I'd recommend checking out this treehouse video on getting children of a node.
For this question we've selected the
<ul>
element with the ID of "rainbow" using this query selector:document.querySelector('[id=rainbow]')
This returns the
<ul>
element but our goal is to apply the colors to the list items nested inside of it, one by one. By using the.children
property on our selection we get an HTML collection that contains the elements that are direct children of the<ul>
. This looks like an array:HTMLCollection(7) [li, li, li, li, li, li, li]
These are the specific list items that we want the colors applied to. We can access these by index -- for instance,
document.querySelector('[id=rainbow]').children[0]
targets the first list item directly.In the solution this HTML collection is stored in the variable
listItems
, solistItems[0]
is the first<li>
. We use the for loop to match the first list item to the first color, then the second list item to the second color, and so on until all of the colors have been applied:Hope this helps :)