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

Amanda Olczak
Amanda Olczak
4,124 Points

New color doesn't show up (just a dark grey circle); .on() isn't allowing new colors to be "selected"

Here's my code:

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

//When clicking on control list items $('.controls li').on('click', 'li', function() { $(this).siblings().removeClass('selected'); //deselect sibling elements $(this).addClass('selected'); //select clicked elements color = $(this).css('background-color') //cache current color });

//When new color is pressed $('#revealColorSelect').click(function() { changeColor(); $('#colorSelect').toggle(); //Show color select or hide the color select }); //Update the new color span function changeColor() { var r = $('#red').val(); var g = $('#green').val(); var b = $('#blue').val();

$('#newColor').css('background-color', 'rgb(' + r + ',' + g + ',' + b + ')');

} //When color sliders change $('input[type=range]').change(changeColor);

//When add color is pressed //Append the color to the controls ul $('#addNewColor').click(function() { var $newColor = $("<li></li>"); $newColor.css('background-color'), $('#newColor').css('background-color'); $('.controls ul').append($newColor); $newColor.click(); //Select the new color

});

What am I doing wrong?

3 Answers

Steven Parker
Steven Parker
229,732 Points

It can be hard to see the issue without the complete project, since JavaScript, HTML, and CSS all work together. You can share everything at once if you make a snapshot of your workspace and provide the link to it.

:point_right: However, in this case it looks like you have a misplaced parenthesis:

//$newColor.css('background-color'), $('#newColor').css('background-color');  <- misplaced
  $newColor.css('background-color', $('#newColor').css('background-color'));  // fixed

Also, the base for your delegate click handler should be the outer list, not the list item:

//$('.controls li').on('click', 'li', function() {  <- delegate based on item
  $('.controls ul').on('click', 'li', function() {  // delegate based on list
Travis Sweeney
Travis Sweeney
10,973 Points

It looks to me like you have a misplaced closing parentheses on the following line:

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

You want to change the value of the $newColor's background-color to the #newColor's background color. So try moving the latter into the parentheses like so:

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

Travis Sweeney
Travis Sweeney
10,973 Points

As far as .on not allowing new colors to be selected, you need to change the parent selector in your statement. You have .on binded to a click event on the "li" child, but you still have the parent in your code as ".controls li". So the .on method is looking for a "li" child of a ".controls li" parent. In other words, it's looking for a "li" nested within another "li". Sorry if the way I'm explaining this is confusing, as I'm still learning myself. Just change ".controls li" to ".controls". Hope this helps!