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

Niyamat Almass
Niyamat Almass
8,176 Points

What's wrong with my code?

I don't figure out what's wrong in my code.this code doesn't working

app.js
var color = $(".selected").css("background-color");


$(".controls").on("click", "li", function () {
$(this).siblings().removeClass("selected");
  $(this).addClass("selected");


color = $(this).css("background-color");
});
changeColor();
$("#revealColorSelect").click(function() {
$("#colorSelect").toggle();


});


function changeColor () {
  var r = $("#red").val();
   var g = $("#green").val();
   var b = $("#blue").val();


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


}
 $("input[type=range]").change(changeColor);


$("#addNewColor").click(function() {
var $newColor = $("<li></li>");
  $newColor.css("background-color", $("#newColor").css("background-color") );
$(".controls ul").append($newColor);
  $newColor.click();

});

1 Answer

Joseph Neenan
Joseph Neenan
3,363 Points

This is a scope issue! You've written your function to change color correctly but it looks like it isn't being called in the right place. Try moving changeColor(); inside the #revealColorSelect.click function as below.

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

        $(this).siblings().removeClass("selected");

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

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

    });


    $("#revealColorSelect").click(function() {

        changeColor();

        $("#colorSelect").toggle();
    });

Hope this helps =]

Niyamat Almass
Niyamat Almass
8,176 Points

Thank you! Joseph Neenan .That's right.