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!

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 5

Shunsuke Taguchi
Shunsuke Taguchi
9,380 Points

I followed teacher but my code doesn't work at all. Can someone take a look my code please?

var color = $(".selected").css("background-color");
var $canvas = ("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent;
var mouseDown = false;

//When clicking on control list items
$(".controls").on("click", "li", function(){
   //Deselect sibling elements        
  //Select clicked elements 
  //cache the color
  color = $(this).css("background-color");

//When new color is pressed 
  //show color select or hide 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]").on("input", changeColor);

//when add color is pressed 
  //append the color to the controls ul
  var $newColor = $("<li></li>");
  $newColor.css("background-color", $("#newColor").css("background-color"));
  $(".controls ul").append($newColor);
  //select new color

//On mouth events on the canvas
  lastEvent = e;
  mouseDown = true;
  //Draw lines
  if(mouseDown) {
  context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
  context.lineTo(e.offsetX, e.offsetY);
  context.strokeStyle = color;
  lastEvent = e;
  mouseDown = false;

1 Answer

Steven Parker
Steven Parker
227,167 Points

One little typo...

:point_right: You're missing a dollar-sign on line 2:

var $canvas = $("canvas");
Jacob Tollette
Jacob Tollette
27,884 Points

It's always one little typo...

Shunsuke Taguchi
Shunsuke Taguchi
9,380 Points

Thanks so much, Steven and Jacob!