HTML DOM Document links
Example
Number of links in the document:
Get the URL of the first link in the document:
Get the URL of the first link in the document:
More examples below.
Description
The links
property returns a collection of all links in the document.
The links
property returns an HTMLCollection.
The links
property is read only.
The links in the collection represents <a> and <area> elements with an href attribute.
See Also:
HTMLCollection
An HTMLCollection is an array-like collection (list) of HTML elements.
The length Property returns the number of elements in the collection.
The elements can be accessed by index (starts at 0).
An HTMLCollection is live. It is automatically updated when the document is changed.
Syntax
Properties
Methods
Returns
null
if the index is out of range.
Returns
null
if the index is out of range.
Returns
null
if the id does not exist.
Return Value
All <a> and <area> elements in the document.
The elements are sorted as they appear in the document.
More Examples
Get the URL of the element with id="myLink":
Add a red border to the first link in the document:
Loop over all links and output the URL (href) of each:
let text = "";
for (let i = 0; i < links.length; i++) {
text += links[i].href + "<br>";
}
Browser Support
document.links
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |