document.getElementById not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

document.getElementById not working

The title says it all. And yes, the script is deferred until the DOM is loaded. The segment of code which doesnt work: function init() { context = myCanvas.getContext('2d'); setInterval(draw,10); randomerase; var body = document.getElementById('myCanvas'); var drawx = body.innerWidth; var drawy = body.innerHeight; } The var body returns undefined, but I'm not sure why. I know it isn't a DOM loading problem because I have another var earlier which references elements by class. I have also tried with double quotes ("") and they do not work either. Any help is appreciated.

20th Oct 2016, 2:10 PM
Jayce Rushton
1 Answer
0
make sure that a id belong to myCanvas. Try it with a class - getElementsByClassName. If it doesnt get the id it shouldnt be undefined. It should be null
20th Oct 2016, 3:58 PM
Angelos Kirkos