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

Kyle Cuthbert
Kyle Cuthbert
4,933 Points

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

Can someone point out my error please?

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];    
<!DOCTYPE html>
    <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>
    <script src="js/app.js"></script>

1 Answer

Emmanuel C
Emmanuel C
8,730 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;