HTML | DOM attributes Property

The attributes property in HTML DOM returns the group of node attributes specified by NamedNodeMap objects. The NamedNodeMap object represents the collection of attribute objects and can be accessed by index number. The index number starts with 0.

Syntax:

node.attributes

Return Value: It returns the NamedNodeMap object which is the collection of nodes.

Note: In Internet Explorer 8 and earlier versions, the attributes property will return a collection of all possible attributes for an element that can result in higher value than expected.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>
            HTML DOM attributes Property
        </title>
    </head>
      
    <body>
      
        <!-- Setting up an image -->
        <img id = "GFG" src
          
        <br>
        <button onclick = "myGeeks()">
            DOM attributes property
        </button>
          
        <p id = "demo"></p>
          
        <script>
            function myGeeks() {
                
            // It returns the number of nodes
            var x = document.getElementById("GFG").attributes.length; 
              
            // Display the number of nodes
            document.getElementById("demo").innerHTML = x; 
        }
        </script>
    </body>
</html>                    

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>
            HTML DOM attributes Property
        </title>
    </head>
      
    <body>
        <h2>
            HTML DOM attributes Property
        </h2>
          
        <button id = "GFG" onclick = "myGeeks()">
            Click Here!
        </button>
          
        <br><br>
          
         <span>
            Button element attributes: 
        </span>
          
        <span id="sudo"></span>
          
        <script>
            function myGeeks() {
                  
            // It returns the number of nodes    
            var gfg = document.getElementById("GFG").attributes.length;
              
            // Display the number of nodes
            document.getElementById("sudo").innerHTML = gfg;
        }
        </script>
    </body>
</html>                    

chevron_right


Output:

Supported browsers The browser supported by DOM attributes property are listed below:

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


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.