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

Marwa Rashad
Marwa Rashad
4,198 Points

What is the solution of the code challenge?

I know the solution is straightforward but I keep getting an error using this code; any help?

<!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>
let listItems = document.querySelector("#rainbow");
const colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];

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

1 Answer

Chris Shaw
Chris Shaw
26,636 Points

Hi Marwa Rashad,

You're very close!

When a challenge refers to an HTMLCollection we can't use singular methods such as queryElement because it only returns the first one it finds in the DOM. Instead, we need to use querySelectorAll which will give us the collection we need.

Along with this, we also need to select the LI elements in the #rainbow element.

let listItems = document.querySelectorAll("#rainbow li");

Happy coding!