Find out how many <a> elements there are in the document:
The result of x will be:
More "Try it Yourself" examples below.
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.
Collection | |||||
---|---|---|---|---|---|
anchors | Yes | Yes | Yes | Yes | Yes |
Property | Description |
---|---|
length | Returns the number of <a> elements in the collection. Note: This property is read-only |
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 |
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 |
Get the HTML content of the first <a> element in the document:
The result of x will be: