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) Getting a Handle on the DOM Selecting Multiple Elements

Muhammad Raza
Muhammad Raza
3,678 Points

My code does'nt seem to be working, would you please be able to look at it.

I have selected the id throught document getELementById method but maybe i have not understood the question correctly for to do anything further.

js/app.js
var listItems;
var colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];

for(var i = 0; i < colors.length; i ++) {
  listItems[i].style.color = colors[i];    
}
index.html
<!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>

4 Answers

Grzegorz Zielinski
Grzegorz Zielinski
5,838 Points

As far as I remember Steve is right - this task is all about assigning the value to variable "var listItems", you don't have to change anything in the other part of the code.

var listItems = document.querySelectorAll('li'); // This part is a major task of the challenge.
var colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];

for(var i = 0; i < colors.length; i ++) {
  listItems[i].style.color = colors[i];    
}

You have to use "querySelectorAll('li');" method so you can get full list of "<li>" elements from unordered list, and then by specifying index value in loop give them a color values also specified by index value.

Hope it helps!

you can use the getElement by ID and then i think you also need to select the li .So bestway to go will be listItems=(document.getElementById).querySelectorAll("li")

Steven Parker
Steven Parker
231,269 Points

I don't see where "getElementById" was used in this code. The very top line of code currently declares "listItems" but does not assign anything to it. The challenge asks you to add code to assign a value to it, using a function that will return a collection containing all list items in the "rainbow" list.

var listItems= document.getElementsById("rainbow");
var colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];

for(let i = 0; i < colors.length; i ++) { listItems[i].style.color = colors[i];
}