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

Hossam Khalifa
Hossam Khalifa
17,200 Points

don't understand this cache color line

$(".controls li").click(function(){
//deselect sibiling element 
  $(this).siblings().removeClass("selected");
  //select clicked element
  $(this).addClass("selected");
  //cache current color
  color= $(this).css("background-color");


});

I don't understand what this cache current color line does and why it should be written

2 Answers

color = $(this).css("background-color");

when you click a list item that is a child of an element classed "controls" it will do this in this order.

1) remove the "selected" class from all other list item elements, 2) it will then add the selected class to the list item element that was just clicked. 3) It will then ask the clicked element what it's background-color css is and store it in the variable color and do nothing with it (unless there's more code).

If you were to add an alert(color); underneath that line you'd see the value of the background-color alerted.

Marked as best answer

David Harris
David Harris
6,009 Points

This is the answer I needed to understand what was going on. Thank you so much for this thorough and clear response.