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 2

Yonatan Medan
Yonatan Medan
3,943 Points

if i dont click on the color controls li the stroke is black why is it?

var $colorControls = $(".controls li");
var $newColorButton = $("#revealColorSelect");
var color = $("li .selected").css("backgroun-color");
var context = $("canvas")[0].getContext("2d");
var $canvas = $("canvas");
var lastEvent;
var mousedown =false;

//selecting a color will de select all the other color

$(".controls").on("click","li",function(){
  $(this).choose();
  color = $(this).css("background-color")
  console.log(color)
});
//clicking on new color will show the new color div

$newColorButton.click(function(){
  $("#colorSelect").toggle();
  changeColor();
  $("body").scrollTop(300);
});
// selecting color will show the color on the span


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);


$.fn.choose = function () {
    $(this).addClass("selected").siblings().removeClass("selected");
};

$(document).on("click","#addNewColor", function (){
  alert("hello");
  $(".controls ul").append($("<li></li>").css("background-color",$("#newColor").css("background-color")));
 $(".controls ul li:last").choose();
});


$canvas.mousedown(function(e){
  lastEvent = e;
  mousedown = true;
}).mousemove(function(e){
  if (mousedown){
context.beginPath();
context.moveTo(lastEvent.offsetX,lastEvent.offsetY);
context.lineTo(e.offsetX,e.offsetY);
context.strokeStyle = color;
context.stroke();
lastEvent = e;}
}).mouseup(function(){
mousedown = false
});
Bobby Verlaan
Bobby Verlaan
29,538 Points

I didn't have the time to look at your question yet. But please post your code with markdown (see cheatsheet) in a readable format like below. In that way we can follow along pretty simple. Get back to it later this day if no one else found the solution yet ;-)

If you check your code you find there are missing some semicolons at some lines that should be there. Also check the closing of a function with the syntax of function(){};

var $colorControls = $(".controls li"); 
var $newColorButton = $("#revealColorSelect"); 
var color = $("li .selected").css("backgroun-color"); 
var context = $("canvas")[0].getContext("2d"); 
var $canvas = $("canvas"); 
var lastEvent; 
var mousedown =false;

//selecting a color will de select all the other color

$(".controls").on("click","li",function(){ 
  //clicking on new color will show the new color div
  $(this).choose(); 
  color = $(this).css("background-color") 
  console.log(color) 
}); 

$newColorButton.click(function(){ 
  $("#colorSelect").toggle(); changeColor(); 
  $("body").scrollTop(300); 
}); // selecting color will show the color on the span

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);
$.fn.choose = function () { $(this).addClass("selected").siblings().removeClass("selected"); };
$(document).on("click","#addNewColor", function (){ 
  alert("hello"); 
  $(".controls ul").append(
    $("").css("background-color",
    $("#newColor").css("background-color"))); 
    $(".controls ul li:last").choose(); 
});

$canvas.mousedown(function(e){ 
  lastEvent = e; mousedown = true; 
}).mousemove(function(e){ 
  if (mousedown){ 
    context.beginPath(); 
    context.moveTo(lastEvent.offsetX,lastEvent.offsetY); 
    context.lineTo(e.offsetX,e.offsetY); 
    context.strokeStyle = color; 
    context.stroke(); 
    lastEvent = e;
  } 
}).mouseup(function(){ 
  mousedown = false 
});

Hi Yonatan Medan, I've formatted your question with a code block. Please see the Markdown Cheatsheet (linked below the question/answer/comment text box) for instructions on how to format code blocks.

1 Answer

So you have a couple of typos when you set the initial value of the color variable:

var color = $("li .selected").css("backgroun-color");

Should be:

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