Document anchors Collection - Javascript DOM

Javascript examples for DOM:Document anchors

Description

The anchors collection returns a collection of all <a> elements with name attribute.

Properties

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

Methods

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

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

The following code shows how to Find out how many <a> elements there are in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<a name="html">HTML Tutorial</a><br>
<a name="css">CSS Tutorial</a><br>
<a name="xml">XML Tutorial</a><br>

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

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

<script>
function myFunction() {/*from   w  ww  .  ja va  2  s  . c om*/
    var x = document.anchors[0].innerHTML;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials