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 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.