HTML | DOM anchors Collection

The DOM anchors collection is used to return the collection of all <a> element in a HTML document. It only counts those <a> element that has the name attribute only. The name attribute of anchor element does not support in HTML 5. The elements in the collection are sorted that appear in the sourcecode.

Syntax:

document.anchors

Property: The anchor collection property contains a value length which returns the number of <a> elements in the document.

Methods: The DOM anchors collection contains three methods which are given below:

  • [index]: It is used to return the <a> element of selected index. The index value starts with 0. It returns NULL if the index value is out of range.
  • item(index): It is used to return the <a> element of selected index. The index value starts with 0. It returns NULL if the index value is out of range.
  • namedItem(id): It is used to return the <a> element from the collection with given id attribute. It returns NULL if the id is not valid.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>DOM anchors collection</title>
        <style>
            .gfg {
                font-size:40px;
                font-weight:bold;
                color:green;
            }
            body {
                text-align:center;
                font-family:Times;
            }
        </style>
    </head>
    <body>
        <a name="" class="gfg">GeeksforGeeks</a>
        <h2>DOM anchors Collection</h2>
        <a name="">GeeksforGeeks</a><br>
        <a name="">Sudo Placement</a><br>
        <a name="">GFG</a><br><br>
        <button onclick="Geeks()">Submit</button>
        <p id="sudo"></p>
        <script>
            function Geeks() {
                var x = document.anchors.length;
                document.getElementById("sudo").innerHTML = 
                "Number of Anchors elements: " + x;
            }
        </script>
    </body>
</html>                    

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>DOM anchors collection</title>
        <style>
            .gfg {
                font-size:40px;
                font-weight:bold;
                color:green;
            }
            body {
                text-align:center;
                font-family:Times;
            }
        </style>
    </head>
    <body>
        <a name="" class="gfg">GeeksforGeeks</a>
        <h2>DOM anchors Collection</h2>
        <a name="">GeeksforGeeks</a><br>
        <a name="">Sudo Placement</a><br>
        <a name="">GFG</a><br><br>
        <button onclick="Geeks()">Submit</button>
        <p id="sudo"></p>
        <script>
            function Geeks() {
                var x = document.anchors[2].innerHTML;
                document.getElementById("sudo").innerHTML = 
                "Second Anchor element: " + x;
            }
        </script>
    </body>
</html>                    

chevron_right


Output:

Supported Browsers: The browser supported by DOM anchors collection are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.