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

querySlectorAll is not working

document.querySelectorAll is not working wht when i try to run the below code it doesnt change the color of all li to blue

document.querySelectorAll('li').style.color = "red";

Tai Jun Jie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tai Jun Jie
Full Stack JavaScript Techdegree Graduate 23,907 Points

You need to use a for loop to loop through all the "li" as querySelectorAll returns you an array.

Eg:

const liArray = document.querySelectorAll('li');
for (let i = 0; i < liArray.length ; i ++ ) {
      liArray[i].style.color = 'red';
}

1 Answer

Steven Parker
Steven Parker
231,141 Points

The "querySelectorAll" retuns a collection of elements, which has no "style" attribute of its own. But as shown in the example by Jun, you can use a loop to set the attribute of each element in the collection.

But it still won't change the color to blue unless you also change the color name from "red" to "blue". :see_no_evil: