[Chartjs]-$(…).getContext("2d") is not a function when using chart.js

8👍

This has been answered already but, in case someone stumbles upon this post, to get the function getContext to work make sure you are rendering the chart inside a <canvas id='myChart'> </canvas> element.

4👍

when attempting to retrieve getContext(“2d”) on an element using jquery, you need to alter the syntax slightly so that the dom ‘node’ is returned, rather than a jquery object. The correct syntax in this case is;

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

note the addition of [0]

0👍

getContext is a method for javascript, use:

document.getElementById('myChart').getContext('2d');

for JQuery use:

$("#myChart").get(0).getContext("2d");

Leave a comment