3

What is better and what is the difference (performance, browser support...)?

I thought that DOM elements that have an id automatically create in a window object so you don't need to find them in DOM through getElementById. Am I right and what problems it could bring? I can't find any documentation on such a possibility of js window object.

Examples with window:

window['elementId'];
window.elementId;

The same result as getElementById and querySelector

Igor
  • 541
  • 1
  • 9
  • 16
  • 2
    Creating properties of the window object using element IDs was a very bad idea introduced in early IE (version 3?). It is supported by other browsers under the guise of legacy support, just don't do it. E.g. see [*Why JS function name conflicts with element ID?*](https://stackoverflow.com/questions/9158238/why-js-function-name-conflicts-with-element-id) and [*Do DOM tree elements with ids become global variables?*](https://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables) – RobG Oct 22 '18 at 08:27
  • 1
    Also [*Why don't we just use element IDs as identifiers in JavaScript?*](https://stackoverflow.com/questions/25325221/why-dont-we-just-use-element-ids-as-identifiers-in-javascript) Choose your dupe. ;-) – RobG Oct 22 '18 at 08:32

0 Answers0