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 5

Shreyas Sreenivasa
Shreyas Sreenivasa
7,804 Points

Color does snot change

Hey there! I think i have done most of the code exactly the same but the color of my pen doe snot change.

My jQuery:

//Problem: No user interaction causes no change to application
//Solution: When user interacts cause changes appropriately

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

$('.controls').on( 'click', 'li', function() {
   $(this).siblings().removeClass('selected');
   $(this).addClass('selected');
   color = $(this).css('background-color');
});

$($button).click( function() {
  changeColor();
  $('#colorSelect').toggle(); 
});

function changeColor() {
  var r = $('#red').val();
  var g = $('#green').val();
  var b = $('#blue').val();
  $bgcolor = 'rgb(' + r + ',' + g + ',' + b + ')';
  $('#newColor').css('background-color', $bgcolor);
}

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

function addColor() {
  var $newColor = $('<li></li>');
  $newColor.css('background-color', $bgcolor);
  $newColor.click();
  $('.controls ul').append($newColor);
}

$('#addNewColor').click(addColor);

$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;
}).mouseleave(function() {
  mouseDown = false;
});

My JSFiddle: https://jsfiddle.net/jy8020uk/

Thank You in advance!

1 Answer

Justin Coen
seal-mask
.a{fill-rule:evenodd;}techdegree
Justin Coen
Full Stack JavaScript Techdegree Student 12,829 Points

You have a typo on your $color variable

$('.controls').on( 'click', 'li', function() {
   $(this).siblings().removeClass('selected');
   $(this).addClass('selected');
   color = $(this).css('background-color');
});

Change color to $color