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 3

lesak
lesak
6,260 Points

Can someone explain me this line of code?

$newColor.css("background-color", $("#newColor").css("background-color"))

I understand that we're changing the background color of the variable $newColor, but adding the value $("#newColor").css("background-color") throws me off.

I think I've seen something like this in sci-fi time loop scenarios, or looking into a mirror in front of a mirror that goes on for infiniti...

3 Answers

$newColor.css("background-color",$("#newColor").css("background-color"));

What is happening here is explained better than me in the prior video: perform part 2. He explains that the css method takes two arguments, the "getter" and the "setter" so what it is saying is get the css style background and apply it to the the id newColor background-color. I hope this makes sense. I would suggest watching the prior video as he I think explains its best.

lesak
lesak
6,260 Points

Thanks Jacob. I understand it now. I think what confused me was the variable name $newColor was pretty similar to the jquery object name. All clear. Thanks!

I agree, the var name is a little confusing. I'm glad I could help.

the variable name is the jquery object, its the id name that is the same. the id name is stated with a # sign, and its the same name as the variable jquery object

oh...now I get it 0.o) $newColor is not $("#newColor")

John, that is correct. This is a perfect example of why its important to clearly and properly name your variables. As we can see naming your variable the same as the id name of the div might not be the best choice, for people who are beginning to learn jQuery. So outside of knowing the difference between $newColor and #newColor, Its more important to name variables that are clear, concise, and can be easily distinguished.

lesak
lesak
6,260 Points

I just had to read that sentence a few times but I think I've digested it now. Thanks heaps.