JavaScript document anchors Property Tutorial

In this section, we will learn what the document anchors property is and how to use it in JavaScript.

What is document anchors Property in JavaScript?

The JavaScript document anchors property is used to return a collection that contains the entire <a> elements in a page that have set a value for their `name` attribute.

Note: this means if there’s an <a> element in a page that doesn’t have the `name` attribute, it won’t be included in the returned a collection of this property.

document anchors Property Syntax:


document anchors Property Parameters

The anchors property is read-only. So this means you can’t set an argument for the property.

document anchors Property Return Value

The return value of this property is a reference to a collection object that contains the entire <a> elements on a page that has a value set for their `name` attribute.

Example: using document anchors Property in JavaScript

See the Pen using document anchors Property in JavaScript by Omid Dehghan (@odchan1) on CodePen.

How Does JavaScript document anchors Property Work?

As you can see, here we have 4 <a> elements, but the last one does not have the `name` attribute associated with it. For that reason, it was not included in the items of the collection that were returned after calling the anchors property.


Top Technologies