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

CSS jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 5

Mousedown not working

I get "undefined is not a function" error when using mousedown event. Here is my code:

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


// error is triggered here:
canvas.mousedown(function(event){
  console.log('mouse down');
});

2 Answers

I figured out what's wrong just not sure why.

// after swapping ' [0] '  it works!
var canvas = $('canvas');
var context = canvas[0].getContext('2d');
canvas.mousedown(function (event) {
});
Greg Kaleka
Greg Kaleka
39,021 Points

Huh. That's super weird. Hope someone can explain this for us hah.

EDIT Oh! I get it. you need the .mousedown function to be called on the actual canvas, not just the javascript representation of the element (which is canvas[0]).

Greg Kaleka
Greg Kaleka
39,021 Points

You're not using the "event" variable you passed the function. Check out how Andrew writes the function in the video.

Doesn't work with or without passing "event" variable.