I was reviewing some code and thought I had found an error in relation to a missing getElementById in the JavaScript code. However, the output appeared to be in order. Working backwards I found this
With HTML code as follows;
<canvas id="display" width="600" height="600"></canvas>
<div id="stage" width="100" height="200"> </div>
<canvas id="hud" width="200"> </canvas>
..and JavaScript as below;
console.log(display.width); // displays 600
console.log(stage.width); // displays undefined as expected
console.log(hud.width); // displays 200
Any Id against the canvas element seems to need no formal declaration for the JavaScript to be able to find it within the HTML.
Am I missing something well known here? I note even on W3Schools that there is usually a variable declared to refer to the element ID.
Thanks in advance.