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

console lastEvent is undefined

var color = $(".selected").css("background-color");        //my code wrong...wa ([.red, .blue, .yellow]);


var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent; 



$(".controls").on("click", "li", function(){
  $(this).siblings().removeClass("selected"); //siblings traverses to the left of a line. we dont put the dot for selected because removeClass already does that.
  $(this).addClass("selected");

  // mycode wrong $(this).next
  color = $(this).css("background-color"); // catches the color when we draw lines later.
});

$("#revealColorSelect").click(function(){;   // see button ids get #, div classes get .  ??? 
    changeColor();   // if this is not here it wont work
    $("#colorSelect").toggle();     // toggle effect display or hides all inside the div including sliders. notice div id under button also has #


});

function changeColor(){
  //window.changeColor
  var r = $("#red").val();
  var g = $("#green").val();
  var b = $("#blue").val();  
  $("#newColor").css("background-color", "rgb(" + r + ", " + g + ", " + b + ")");  
};  // not sure why the "rgb..' string is there. because .css can get or set values 
//oh i see creates <span id="newColor" style="background-color: rgb(255, 208, 255);">       

$("input[type=range]").change(changeColor);  //css selector that only selects type of range, input is in index.html and has type="range", with select box change will select method


$("#addNewColor").click(function(){
  var $newColor = $("<li></li>");
  $newColor.css("background-color", $("newColor").css("background-color"));
  // mine wrong doesnt work...   $(".canvas").append("#newColor");

  $(".controls ul").append($newColor);

  $newColor.click();
});


//On mouse events on the canvas
   //Draw lines



$canvas.mousedown(function(event){
  lastEvent = event;
});
  context.beginPath();
  context.moveTo(10, 10); 
  context.lineTo(20, 10);  
  context.lineTo(20, 20);
  context.lineTo(10, 20);
  context.closePath(); 
  context.stroke();

In the console it appears that the lastEvent variable is unable to bring me anything that the instructor is getting. It is an error comes up as undefined..

5 Answers

Adam Pengh
Adam Pengh
29,881 Points

Event is a reserved word in JavaScript, so you would need to use something other that the word event in your code, such as just 'e' like he uses in the video. Event is also passed automatically, so you can use this:

$canvas.mousedown(function(){
  lastEvent = event;
});

or this:

$canvas.mousedown(function(e){
  lastEvent = e;
});
Steven Parker
Steven Parker
229,785 Points

I'm not seeing that. But it looks like you're in the middle of some changes from the video, so I wouldn't be concerned until you reach a point of completion.

Also, this code works in conjunction with some HTML and probably some CSS. But you can share your entire workspace if you make a [snapshot] https://teamtreehouse.com/library/previews-and-snapshots) and provide the link.

https://w.trhou.se/pn5ghpiy6w

yes I had already done it that way and it didn't work.

I believe the reason is because you need to left-click on the white canvas first, and then push down on your keyboard. Afterwards, type lastEvent into the console and what the instructor said should come up.

This course has been a confusing mess. I am so glad I only have a few minutes left.

still didnt work https://teamtreehouse.com/workspaces/17795512# there clone my workspace and see.