Skip to content

Documentation does not warn against using element IDs as global properties #39339

@krazy-j

Description

@krazy-j

MDN URL

https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/id

What specific section or headline is this issue about?

Description

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() or document.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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Content:HTMLHypertext Markup Language docsaccepting PRFeel free to open a PR to resolve this issuegoal: best practices(Experimental label) Issues about content displaying unusual approaches.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions