jQuery equivalent of getting the context of a Canvas

Try:

$("#canvas")[0].getContext('2d');

jQuery exposes the actual DOM element in numeric indexes, where you can perform normal JavaScript/DOM functions.


I have also seen that it's often preferred to use .get(0) to reference a jquery target as HTML element:

var myCanvasElem = $("#canvas").get(0);

Perhaps to help avoid any potential null object references since jquery returns null as an object but working with the element from .get(0) may not fail so silently... You can easily check if the canvas was found first before .get(0) like

if( $("#canvas").length ) ctx = $("#canvas").get(0).getContext('2d');
else console.log('Error: Canvas not found with selector #canvas');