Latest web development tutorials
 

HTML DOM anchors Collection

< Document Object

Example

Find out how many <a> elements there are in the document:

var x = document.anchors.length;

The result of x will be:

3
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The anchors collection returns a collection of all <a> elements in the document that have a name attribute.

Note: The name attribute of the <a> element is not supported in HTML5.

The elements in the collection are sorted as they appear in the source code.

Tip: Also look at the Anchor Object and document.links.


Browser Support

Collection
anchors Yes Yes Yes Yes Yes

Syntax

document.anchors

Properties

Property Description
length Returns the number of <a> elements in the collection.

Note: This property is read-only

Methods

Method Description
[index] Returns the <a> element from the collection with the specified index (starts at 0).

Note: Returns null if the index number is out of range
item(index) Returns the <a> element from the collection with the specified index (starts at 0).

Note: Returns null if the index number is out of range
namedItem(id) Returns the <a> element from the collection with the specified id.

Note: Returns null if the id does not exist

Technical Details

DOM Version: Core Level 1 Document Object
Return Value: An HTMLCollection Object, representing all <a> elements in the document that have a name attribute. The elements in the collection are sorted as they appear in the source code

Examples

More Examples

Example

Get the HTML content of the first <a> element in the document:

var x = document.anchors[0].innerHTML;

The result of x will be:

HTML Tutorial
Try it Yourself »

< Document Object