JavaScript JavaScript and the DOM Getting a Handle on the DOM Selecting Multiple Elements

Kyle Cuthbert
Kyle Cuthbert
4,065 Points

Im getting a 'Bummer' message asking if I listed all 7 items. I think I have

Can someone point out my error please?

js/app.js
var listItems = document.getElementById("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];    
}
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>

1 Answer

Emmanuel C
Emmanuel C
6,919 Points

They want you to set listItems to the list of children, that way you dont have to change how the for loop accesses each child.

var listItems = document.getElementById("rainbow").children;