Javascript Reference - HTML DOM anchors Collection








The anchors collection returns a list of all the anchors in the current document.

Browser Support

Collection Yes Yes Yes Yes Yes

Syntax

document.anchors

Return Value

A NodeList object representing a collection of anchors.

Example

The following code shows how to get the number of anchors in the document.


<!DOCTYPE html>
<html>
<body>
<a name="html">HTML</a><br>
<a name="css">CSS</a><br>
<a name="xml">XML</a><br>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<!-- w  ww.j av a  2  s  .c  om-->
<script>
function myFunction() {
    var x = document.getElementById("demo");
    x.innerHTML = document.anchors.length;
}
</script>


</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the innerHTML of the first anchor in the document.


<!DOCTYPE html>
<html>
<body>
<!-- www .j  av a2 s . co m-->
<a name="html">HTML</a><br>
<a name="css">CSS</a><br>
<a name="xml">XML</a><br>

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

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

<script>
function myFunction()
{
    var x=document.getElementById("demo");
    x.innerHTML=document.anchors[0].innerHTML;
}
</script>

</body>
</html>

The code above is rendered as follows: