DEV Community


Discussion on: DOM elements with ID's are global variables

nathanhinchey profile image
Nathan Hinchey

This is specified the WHATWG spec,

Note that they explicitly point out that this is a bad idea to use in your code:

As a general rule, relying on this will lead to brittle code. Which IDs end up mapping to this API can vary over time, as new features are added to the Web platform, for example. Instead of this, use document.getElementById() or document.querySelector().