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 trialvryiziilos
Full Stack JavaScript Techdegree Student 23,052 PointsMousedown 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
vryiziilos
Full Stack JavaScript Techdegree Student 23,052 PointsI 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
39,021 PointsYou're not using the "event" variable you passed the function. Check out how Andrew writes the function in the video.
vryiziilos
Full Stack JavaScript Techdegree Student 23,052 PointsDoesn't work with or without passing "event" variable.
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsHuh. 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]).