HTML - DOM Document anchors Property



HTML DOM document anchors property is a read only property which lists all the <a> tag with name attribute in the document. This property is deprecated.

It lists anchor tags with name attribute and since name attribute is no longer supported in HTML 5, anchors property is no longer recommended to use. You can use it's alternate way getElementsByTagName('a') to get the same results.

Syntax

document.anchors;

Return value

It return an HTML collection of all <a> with name attribute.

Examples of HTML DOM Document 'anchors' Property

The following examples illustrtas use of anchors property.

Get the number of anchor Elements

Here, in the following example anchors property returns the number of <a> with name attribute.

<!DOCTYPE html>
<html>
<head>
    <title>HTML DOM document anchors Property</title>
</head>
<body>
    <a name="tutorix">Tutorix</a>
    <br>
    <a name="tutorialspoint">Tutorialspoint</a>
    <br>
    <a> I am without name attribute</a>
    <p>Number of anchor tags with name attribute :</p>
    <p id="anchors"></p>
    <script>
        let num = document.anchors.length;
        document.getElementById("anchors").innerHTML =num;
    </script>
</body>
</html>

Alternate way to get number of anchor Elements

In this example, getElementsByTagName is used as altrnate way to get the number of of anchor elements.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document anchors Property</title>
</head>
<body>
    <a name="tutorix">Tutorix</a>
    <br>
    <a name="tutorialspoint">Tutorialspoint</a>
    <br>
    <a> I am without name attribute</a>
    <p>Click to get the number of anchor element.</p>
    <button onclick="fun()">Click me</button>
    <p id="anchors"></p>
    <script>
        function fun() {
            let num = document.getElementsByTagName("a").length;
            document.getElementById("anchors").innerHTML = num;
        }
    </script>
</body>
</html>

Get the content of first anchor Element

The following example returns the content of first <a> element.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document anchors Property</title>
</head>
<body>
    <a name="tutorix">Tutorix</a>
    <br>
    <a name="tutorialspoint">Tutorialspoint</a>
    <br>
    <a> I am without name attribute</a>
    <p>Click to get the content of first anchor element.</p>
    <button onclick="fun()">Click me</button>
    <p id="anchors"></p>
    <script>
        function fun() {
            let num = document.anchors[0].innerHTML;
            document.getElementById("anchors").innerHTML = num;
        }
    </script>
</body>
</html>

Alternate way to get content of first anchor Element

In this example, getElementsByTagName is used as altrnate way to get the content of first anchor element.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document anchors Property</title>
</head>
<body>
    <a name="tutorix">Tutorix</a>
    <br>
    <a name="tutorialspoint">Tutorialspoint</a>
    <br>
    <a> I am without name attribute</a>
    <p>Click to get the content of first <a> element.</p>
    <button onclick="fun()">Click me</button>
    <p id="anchors"></p>
    <script>
        function fun() {
            let num = document.getElementsByTagName("a")[0].innerHTML;
            document.getElementById("anchors").innerHTML = num;
        }
    </script>
</body>
</html>

Supported Browsers

Property Chrome Edge Firefox Safari Opera
anchors No No No No No
html_dom.htm
Advertisements