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

HTML jQuery Basics (2014) Creating a Simple Drawing Application Preparation

getContext()

I have asked this question to Andrew--the JQuery instructor. If I get his answer, I will post it here.

In the JQuery's simple drawing app, this line of code: var context = $canvas.getContext("2d"); always complaints that getContext is not defined.

Even I used jquery 2.1.1., the problem still exists. If I comment out this line of code, then it compiles.

Any idea?

(from Andrew) If you do $canvas[0] rather than $canvas that should do it. :) [0] grabs he plain old JavaScript representation of the canvas so you can draw context.

2 Answers

Hi Daniel,

$canvas is a jQuery object, not the actual canvas element. Accessing the 1st element (index 0) of this jQuery object will get you the canvas element

$canvas[0]

I'm not sure if this was omitted from the video but it seems to be in the downloadable project files.

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

That's why. Thanks for your clear explanation.

You're welcome.