HTML | DOM getAttributeNode() Method

The getAttributeNode() method in HTML DOM is used to return the attribute node with the specified name of an element, as an attribute object. This function is similar to getAttribute() method but the only difference is that the getAttribute() method returns the value of attribute node not any attribute object.

Syntax:

element.getAttributeNode( attribute_name )

Parameters: This method accepts single parameter attribute_name which is mandatory. This parameter is used to hold the name of attribute.

Return Value: This method returns an attribute object which represents the attribute node.

Example: This example contains two heading element and the task is to display the value of attribute node of second heading element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>
            HTML DOM getAttributeNode() Method
        </title>
          
        <!--script to display the value of
            class attribute -->
        <script>
            function Geeks() {
                var element_name =
                document.getElementsByTagName("H2")[0];
                  
                var attribute = 
                element_name.getAttributeNode("class").value;
                  
                document.getElementById("result").innerHTML
                        = attribute;
            }
        </script>
    </head>
      
    <body>
        <h1 class = "Frst_heading">
            GeeksforGeeks
        </h1>
          
        <h2 class = "Second_heading">
            DOM getAttributeNode() Method
        </h2>
      
        <p>
            Click on the button to display
            the value of attribute node
        </p>
      
        <button onclick = "Geeks()">
            Click Here!
        </button>
      
        <p id = "result"></p>
      
    </body>
</html>                    

chevron_right


Output:
Before click on the button:

After click on the button:

Supported Browsers: The browser supported by DOM getAttributeNode() method are listed below:

  • Google Chrome
  • Internet Explorer
  • Apple Safari
  • 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.