2

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,973
  • 4
  • 29
  • 46

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/multipage/nav-history-apis.html#named-access-on-the-window-object

user3840170
  • 26,597
  • 4
  • 30
  • 62
Jonathan
  • 8,771
  • 4
  • 41
  • 78