Document.anchors is a read-only deprecated instance property that returns an HTMLCollection of anchor elements with a name attribute. Learn how legacy pages used it for table-of-contents scripts, why id-only links are excluded, and how querySelectorAll replaces it—with five examples and try-it labs.
01
Kind
Read-only property
02
Returns
HTMLCollection
03
Status
Deprecated
04
Matches
a[name] only
05
Replace
querySelectorAll
06
Live
Updates with DOM
Fundamentals
Introduction
Before modern CSS and single-page apps, developers marked sections with <a name="chapter-1"></a> and linked to them with href="#chapter-1". To list every in-page target, old scripts read document.anchors.
MDN’s modern recommendation: use document.querySelectorAll("a[name]") if you truly need named anchors, or prefer id attributes on headings and elements with href="#section-id".
💡
Important compatibility note
For backwards compatibility, document.anchors only includes anchors created with the name attribute—not those created with id alone. See MDN Document: anchors.
Document.anchors is deprecated but still implemented for compatibility in major browsers. MDN recommends querySelectorAll('a[name]') or id-based fragment links instead. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Deprecated · Legacy
Document.anchors
Legacy HTMLCollection of named anchor elements — use querySelectorAll or id fragments in new code.
LegacyCompatibility only
Google ChromeCompatibility support · prefer modern APIs
Legacy support
Mozilla FirefoxCompatibility support
Legacy support
Apple SafariCompatibility support
Legacy support
Microsoft EdgeChromium compatibility layer
Legacy support
OperaFollow Chromium behavior
Legacy support
Internet ExplorerOriginal legacy target
Legacy support
Document.anchorsAvoid in new code
Bottom line: Recognize document.anchors in old TOC scripts. For new in-page navigation, use id attributes on headings and querySelectorAll when needed.
Wrap Up
Conclusion
Document.anchors is a deprecated read-only collection of named <a> elements. It is useful for understanding legacy table-of-contents scripts—not for building new features. Use id attributes and modern selectors instead.
Use querySelectorAll("a[name]") if you must query named anchors
Replace document.anchors when you touch legacy files
Build TOCs from semantic heading structure
Test fragment links with location.hash
❌ Don’t
Add new <a name="..."> empty anchors in modern pages
Assume id-only anchors appear in document.anchors
Confuse anchors with links or all <a> tags
Treat HTMLCollection as a real Array without converting
Rely on deprecated collections in new projects
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.anchors
Deprecated named-anchor collection — prefer ids and querySelectorAll.
5
Core concepts
🔗01
Returns
HTMLCollection
API
⚠️02
Status
deprecated
Legacy
🔍03
Matches
a[name]
Selector
🔢04
Excludes
id-only anchors
MDN
🛠05
Replace
querySelectorAll
Modern
❓ Frequently Asked Questions
A read-only HTMLCollection of every anchor element in the document that has a name attribute — legacy in-page link targets such as <a name="section">.
Yes. MDN marks Document.anchors as deprecated. Use document.querySelectorAll('a[name]') or modern id-based fragment links (#id) in new code.
No. For backwards compatibility, the collection only includes anchors created with the name attribute, not those created with id alone.
document.anchors lists named anchor elements (<a name="...">). document.links lists hyperlink elements (<a href="...">). A single <a> can appear in both if it has name and href.
Yes. You can use for...of, a classic for loop with length and index, or spread into an array. The collection is live — it updates when matching anchors are added or removed.
No. Prefer querySelectorAll('a[name]') when you truly need named anchors, or use id attributes with href="#id" for in-page navigation.
Did you know?
The HTML name attribute on <a> was the original way to mark jump targets before id fragments became universal. That is why document.anchors still filters by name even though modern pages link to id values instead.