1

I was using JSLint and it is saying: 'matte_canvas' is not defined. Obviously, in my javascript code I don't have 'matte_canvas' defined, however, it does output the canvas element in the console.

Here is the code I have:

HTML:

<canvas id="matte_canvas" width="50" height="50" style="background-color:blue;"></canvas>

Javascript:

console.log(matte_canvas);

Here it is on JSFiddle: http://jsfiddle.net/allisonc/rqo5a417/

Can someone please explain to me how it is working?

AllisonC
  • 2,908
  • 4
  • 28
  • 45

1 Answers1

2

ids are also globals (if no other value was assigned to them). Don't rely on this.
This also works for the name attribute on certain elements: a, applet, area, embed, form, frameset, img, and object.

alert(window.matte_canvas);

More info: https://html.spec.whatwg.org/#named-access-on-the-window-object

Jonathan
  • 8,095
  • 4
  • 37
  • 70