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 jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 1

Matt Schaefer
Matt Schaefer
6,039 Points

var color declaration question

I'm confused about how this line gets us the background-color of the "red" class, or whichever <li> initially has the "selected" tag in addition to the class for its color.

When an HTML element has two classes like in the case of "red selected" here does this mean that by getting the background-color of the selected class we are actually pulling the background-color from the red class because the <li> in question has both of those classes?

Thanks!

var color = $(".selected").css("background-color");

1 Answer

Casey Ydenberg
Casey Ydenberg
15,622 Points

Hi Matt,

You've actually getting the color of an element, not a class. JQuery will return whatever element(s) matches the jquery selector .selected. Then the .css method returns the property value currently possessed by that element, irrespective of where it came from in the stylesheet or if it's inline. So the fact that the .selected elements color was set by a different class doesn't actually matter.

Note that if your query matches more than one element, .css will just return the property set on the first one.