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 One Solution

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

for loops and class 'card' from Selecting DOM practice workshop

I don't understand why for loops are needed to apply the styles to all the selected elements.I thought when you getElementsBy something, the styles would apply to all. Also my class 'card' style change isn't working. Here's my code:

https://w.trhou.se/cv7spqe3i1

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Leo Marco Corpuz ! When you use getElementsByClassName or getElementsByTagName etc, notice that the word in there is plural. It's Elements as oppsed to Element. The one case where this is different is getElementById and that's because an id must be unique on a page. It can only be assigned to Γ²ne element so it is only possible for it to return one element.

In all other cases, what is being returned is something very similar to an array of elements. We access them much like an array using subscripting. For example, to target the third unordered list we might use something like document.getElementsByTagName("ul")[2]. So in each of these cases, you will need to loop through that collection and apply the style to each separate element. Who knows? Maybe you have an unordered list with several <li> elements and instead of using one of the built in color names, you're using RGB and for every list item in that list, you want the amount of blue to increase by a certain amount.

If you open up your developer tools, you will see the errors that are causing your problem. The .style property is only available on an individual DOM node. But you are trying to apply it to a collection of DOM nodes.

I took the liberty of reworking your code to show you how I might go about this practice, but only look if you're ready for a spoiler!

You can fork the workspace here

Hope this helps! :sparkles: