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 trialVijayalaxmi vastrad
2,789 Points3rd 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
Siddharth Pandey
7,280 PointsYour 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.
Sean Paulson
6,669 PointsYes 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.
Bozena Jablonski
4,944 PointsBozena Jablonski
4,944 PointsI 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.