Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript One Solution

Vijayalaxmi vastrad
Vijayalaxmi vastrad
2,789 Points

3rd question not working

// 3: Select all elements with the class '.card'. 
//    Set their background color to the color of your choice.
const myClass=document.getElementsByClassName('card');
for(let i=0;i<myClass.length;i++){
myClass[i].style.backgroundColor='pink';
}

2 Answers

Your code is accurate, but you selected an element not a class, to select a class you have to put a '.' in front of the class name.

For example

const myClass=document.getElementsByClassName('.card');
for(let i=0;i<myClass.length;i++){
myClass[i].style.backgroundColor='pink';
}

It should work with this fix.

Bozena Jablonski
Bozena Jablonski
4,944 Points

I thought that you only had to add the '.' before a class name in a querySelector not in getElementsByClassName, as it's already obvious that you're looking for a class. Vijayalaxmi has just copied the teacher here.

Yes the example the instructor is using getElementsByClassName without the dot and his is rendering. mine is only rendering the first two. Also if I use the dot like you suggested it does not work at all.

I got it to work using document.querySelectorAll('.card'); Then use the for loop.