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

Alemneh Asefa
Alemneh Asefa
9,503 Points

New color not being appended to ul (checked $newColor in JS console and it says $newColor is not defiend)

//Problem:No user interation casues no change to application
//Solution:When user interacts cause changes
var color = $(".selected").css("background-color");


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

});
//When new add color is pressed
$("#revealColorSelect").click(function() {
  //Show color select or hide the color select

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

//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]").on("input", changeColor);


//When add color is pressed
$("addNewColor").click(function() {
  //Append the color to the controls ul

  var $newColor = $("<li></li>");
  $newColor.css("background-color", $("#newColor").css("background-color"));
  $(".controls ul").append($newColor);
  //Select the new color
  $newColor.click();

});

//on mouse events on the canvas
  //Draw lines
Jacob Miller
Jacob Miller
12,466 Points

Your code is working for me. Did you change the html at all?

Alemneh Asefa
Alemneh Asefa
9,503 Points

I did not change the html. Is $newColor suppoused to show up as not defiend. Do you get the same when you check $newColor in the chorme dev tools?

1 Answer

Hey Alemneh Asefa,

There are a couple issues with your code. I edited your markup so it will render properly, as well.

The first issue I see is that your click event won't execute properly for "addNewColor" because it's missing the "#" symbol that should be prepended to the selector. So, the selector should be $("#addNewColor").

The second issue is that since $newColor is only defined during the click event, this is the only time you would be able to see its value in the console. It only lives while the click event is happening. Once the click event stops, $newColor is destroyed. If you created $newColor outside the scope of the click event, you'd be able to access it, but since it is declared within the click, you get that "Uncaught ReferenceError" message.

My pleasure!