Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.