MyInternships.in

HTML DOM

HTML DOM anchors Collection

The anchors collection returns a live HTMLCollection of all <a> elements in the document that have a name attribute. It lets JavaScript loop over the page's named anchors and read their details.


Definition and Usage

document.anchors returns an HTMLCollection of every anchor (<a>) element that has a name attribute. Anchors that only have an href but no name are not included. The collection is live, meaning it updates automatically if anchors are added or removed.

Each item in the collection is an anchor element, so you can read properties such as name, href, and innerHTML. Use the length property to count them and bracket or item() access to reach individual anchors.

Syntax

document.anchors

Properties

NameDescription
lengthReturns the number of anchors in the collection
[index]Returns the anchor element at the given index
item(index)Returns the anchor element at the given index
namedItem(name)Returns the anchor element with the given name or id

Example

This page has three named anchors. Click the button to count them and list each anchor's name in the output box.

Count and list named anchors
Example
<!DOCTYPE html>
<html>
<body>
  <a name="intro" href="#">Intro</a> |
  <a name="setup" href="#">Setup</a> |
  <a name="usage" href="#">Usage</a> |
  <a href="#">No name (ignored)</a>

  <button onclick="listAnchors()">List anchors</button>
  <div id="out" style="margin-top:12px;"></div>

  <script>
    function listAnchors() {
      const anchors = document.anchors;
      let text = "Total named anchors: " + anchors.length + "<br>";
      for (let i = 0; i < anchors.length; i++) {
        text += (i + 1) + ". " + anchors[i].name + "<br>";
      }
      document.getElementById("out").innerHTML = text;
    }
  </script>
</body>
</html>

More Examples

You can reach a specific anchor by name with namedItem(). Here we highlight the anchor named "setup".

Access one anchor by name
Example
<!DOCTYPE html>
<html>
<body>
  <a name="home" href="#">Home</a>
  <a name="setup" href="#">Setup</a>
  <button onclick="highlight()">Highlight the setup anchor</button>

  <script>
    function highlight() {
      const a = document.anchors.namedItem("setup");
      a.style.background = "yellow";
      a.style.padding = "2px 6px";
    }
  </script>
</body>
</html>
⚠️

The name attribute on anchors is largely obsolete in modern HTML. Prefer using the id attribute for linking, though document.anchors still only reports anchors with a name attribute.

Key Takeaways

  • document.anchors returns all <a> elements that have a name attribute.
  • It returns a live HTMLCollection you can loop over.
  • Use length to count and [index] or item() to access anchors.
  • namedItem(name) returns a single anchor by its name.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships