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

Ryan Herlihy
Ryan Herlihy
7,448 Points

Cannot select different colors in control list. The code is exactly the same, why isn't it working?

app.js

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

//When clicking colors, they are selected
$('.controls li').click(function() {
  $(this).siblings().removeClass('selected');

  $(this).addClass('selected');

  color = $(this).css('background-color');
});

I didn't modify any of the other files.

geoffrey
geoffrey
28,736 Points

Have you checked the console of your browser, do you see any error ? If you use google chrome, press f12 to display the console. Do that, and tell us the error if there is one.

Ryan Herlihy
Ryan Herlihy
7,448 Points

I ended up having to preview my code from workspace then refresh the page that opens, and then it started working fine. The code was correct but something else was causing the problem, I'm not sure what.

geoffrey
geoffrey
28,736 Points

Ok, that's fine so ;)

If you're using Chrome, try your current code out in a different browser. For it to work in Chrome, I had to change .click to .on("click") like this:

$('.control li').click(function() {

}

to

$('controls li').on("click", function() {

}
geoffrey
geoffrey
28,736 Points

Kevin Kendall , the difference between the two examples you gave is the selector.

.controls li as selector is different than .control li.

The difference between the JQuery method .on('click') and the .click() method, is that with .on('click') you can bind a click event to element added dynamically.

2 Answers

Rodrigo Castro
Rodrigo Castro
15,652 Points

I was having the same problem, I solved it by cleaning the browser's cache (chrome) or by running the preview on incognito mode.

Kyle Vassella
Kyle Vassella
9,033 Points

I'm running Chrome and I've made it a habit to refresh the previewed page every single time I change the code. Even if I change the code, save the file and then click preview to open a brand new preview window, I still refresh that new preview window once before trying to interact with it or look at it.

Must be something to do with the cache of the workspaces or the browser. But I find cmd+R (refresh) is a constant best practices tool when doing these lessons on Chrome. Otherwise, I quite often run into problems just like this (code is correct but still running like it did before my latest changes).