JavaScript Document anchors Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Instance property

What You’ll Learn

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

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.

Related Document tutorials: all, alinkColor, Document constructor.

Understanding Document.anchors

A read-only instance property on Document. Its value is a live HTMLCollection of every <a> element that has a name attribute.

  • ValueHTMLCollection of named anchor elements.
  • Read-only — you do not assign a new collection to document.anchors.
  • Live collection — reflects DOM changes when anchors are added or removed.
  • Indexed accessdocument.anchors[i] or item(i).
  • name only<a id="x"> without name is excluded (MDN).

📝 Syntax

JavaScript
document.anchors

Value

An HTMLCollection of anchor elements with a name attribute.

MDN modern replacement

JavaScript
const namedAnchors = document.querySelectorAll("a[name]");

⚡ Quick Reference

GoalCode / note
Legacy: all named anchorsdocument.anchors
Count (legacy)document.anchors.length
First anchor (legacy)document.anchors[0]
Anchor nameanchor.name
Modern: named anchorsdocument.querySelectorAll("a[name]")
MDN statusDeprecated

🔍 At a Glance

Four facts about document.anchors.

Type
HTMLCollection

Read-only, live

Contains
a[name]

Named anchors

Status
deprecated

Legacy API

Use instead
querySelectorAll

Modern DOM

📋 name vs id for Anchors

MarkupIn document.anchors?Modern approach
<a name="top"></a>YesPrefer id="top" on a heading
<a id="top"></a> (no name)No (MDN)href="#top" still works in browsers
<h2 id="intro">Nohref="#intro" (recommended today)
<a name="x" id="x">YesRedundant—pick one pattern

Examples Gallery

Examples follow MDN Document: anchors. Use View Output or Try It Yourself for each case.

📚 Getting Started

Inspect the legacy collection and walk named anchors.

Example 1 — Check document.anchors.length (MDN)

Count how many named anchor elements exist on the page.

JavaScript
if (document.anchors.length >= 5) {
  console.log("found too many anchors");
} else {
  console.log("anchor count:", document.anchors.length);
}
Try It Yourself

How It Works

length counts only <a name="..."> elements—not every link on the page.

Example 2 — Loop and List Anchor Names

Walk the collection and log each anchor’s name.

JavaScript
const names = [];
for (const anchor of document.anchors) {
  names.push(anchor.name);
}
console.log(names.join(", "));
Try It Yourself

How It Works

HTMLCollection is array-like and iterable with for...of in modern browsers.

📈 Practical Legacy Patterns

Table-of-contents scripts and compatibility gotchas.

Example 3 — Auto Table of Contents (MDN)

Build a TOC list from every named anchor on the page.

JavaScript
const toc = document.getElementById("toc");
for (const anchor of document.anchors) {
  const li = document.createElement("li");
  const link = document.createElement("a");
  link.href = "#" + anchor.name;
  link.textContent = anchor.textContent.trim() || anchor.name;
  li.appendChild(link);
  toc.appendChild(li);
}
Try It Yourself

How It Works

This was a common 1990s–2000s pattern before heading ids and CSS-driven TOCs became standard.

Example 4 — name vs id (MDN Note)

Show that id-only anchors are not in the collection.

JavaScript
// In HTML:
// <a name="legacy"></a>
// <a id="modern-only"></a>

console.log("anchors length:", document.anchors.length);
console.log("includes legacy:", !!document.querySelector('a[name="legacy"]'));
console.log("includes id-only:", !!document.querySelector('a#modern-only'));
Try It Yourself

How It Works

MDN: backwards compatibility limits the collection to name anchors even though href="#id" works with ids today.

Example 5 — Modern Replacement with querySelectorAll("a[name]")

Standard API that matches the legacy collection’s intent.

JavaScript
const modern = document.querySelectorAll("a[name]");
console.log("modern count:", modern.length);
console.log("legacy count:", document.anchors.length);
console.log("same length:", modern.length === document.anchors.length);
Try It Yourself

How It Works

For new table-of-contents or navigation scripts, query headings with id instead of scanning named anchors.

🚀 Common Use Cases

  • Reading legacy intranet pages — recognize TOC scripts using document.anchors.
  • Migrating old scripts — replace with querySelectorAll("a[name]") or id-based links.
  • Teaching DOM history — contrast name anchors with modern fragment ids.
  • Debugging — compare anchors.length vs visible in-page link targets.
  • Not for new apps — use semantic headings with id attributes instead.
  • Documentation sites — auto-generate nav from h2[id], not a[name].

🧠 How document.anchors Fits the DOM

1

Page defines named anchors

<a name="section"> marks in-page targets.

HTML
2

Script reads document.anchors

Browser returns a live HTMLCollection.

Legacy
3

Build TOC or validate count

Loop anchors, read name, create href="#name" links.

Pattern
4

Prefer modern ids today

Use id on headings and querySelectorAll when you must query named anchors.

📝 Notes

  • document.anchors is deprecated (MDN) — avoid in new code.
  • Read-only property; returns a live HTMLCollection, not a plain array.
  • Only includes <a name="..."> — not id-only anchors (MDN).
  • MDN replacement: document.querySelectorAll("a[name]").
  • Do not confuse with document.links (elements with href).
  • Related: all, ownerDocument, String.anchor().

Legacy Browser Support

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.

Legacy Compatibility only
Google Chrome Compatibility support · prefer modern APIs
Legacy support
Mozilla Firefox Compatibility support
Legacy support
Apple Safari Compatibility support
Legacy support
Microsoft Edge Chromium compatibility layer
Legacy support
Opera Follow Chromium behavior
Legacy support
Internet Explorer Original legacy target
Legacy support
Document.anchors Avoid 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.

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.

Continue with applets, ownerDocument, all, Document constructor, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use id on headings for in-page navigation
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about document.anchors

Deprecated named-anchor collection — prefer ids and querySelectorAll.

5
Core concepts
⚠️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.

Next: applets

Learn the deprecated empty HTMLCollection behind document.applets.

applets →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful