-
Notifications
You must be signed in to change notification settings - Fork 23.2k
Documentation does not warn against using element IDs as global properties #39339
Description
MDN URL
https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/id
What specific section or headline is this issue about?
What information was incorrect, unhelpful, or incomplete?
The documentation for the id attribute describes how "Elements with ID attributes are available as global properties", and gives an example of how it can be used with JavaScript, but there is no mention of how it might be bad practice to reference elements this way.
As answers on Stack Overflow have said (see references), this "feature" came to be because IE started it, and other browsers had to follow for compatibility. This functionality is part of the HTML specification, but even the specification advises against using it:
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()ordocument.querySelector().
What did you expect to see?
I would expect to see some sort of note that advises against using this feature, just as the specification does.
Do you have any supporting links, references, or citations?
Stack Overflow:
- Answer to "Do DOM tree elements with IDs become global properties?"
- Answer to "Is there a spec that the id of elements should be made global variable?"
Do you have anything more you want to share?
Documenting this sort of behavior without any warnings seems like a mistake to me. It concerns me that this change has remained here for two years.
MDN metadata
Page report details
- Folder:
en-us/web/html/reference/global_attributes/id - MDN URL: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/id
- GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/html/reference/global_attributes/id/index.md
- Last commit: e9b6cd1
- Document last modified: 2025-04-10T14:56:07.000Z