Document links Collection - Javascript DOM

Javascript examples for DOM:Document links

Description

The links collection returns all links in the document representing <a> elements and/or <area> elements with a href attribute.

If the element is missing the href attribute, nothing is returned.

Syntax

Properties

Property Description
length Returns the number of <a> and/or <area> elements in the collection.

Methods

MethodDescription
[index] <a> and/or <area> element with the specified index (starts at 0).
item(index) <a> and/or <area> element with the specified index (starts at 0).
namedItem(id) <a> and/or <area> element with the specified id.

Technical Details

Return Value:

An HTMLCollection Object, representing all <a> elements and/or <area> elements in the document.

The following code shows how to Find out how many links there are in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>
  <a href="/html/default.asp">HTML</a><br>
  <a href="/css/default.asp">CSS</a>
</p>//from   w  w  w.j  a  va2 s . com

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.links[0].style.border = "5px solid red";
}
</script>

</body>
</html>

Related Tutorials