HTML | DOM getAttribute() Method

DOM getAttribute() method is used to get the value of the attribute of the element. By specifying the name of the attribute, it can get the value of that element. To get the values from non-standard attributes we can use getAttribute() method.

Syntax: It returns the string containing the attribute’s value.

Object.getAttribute(attributename)

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM getAttribute() Method </title>
</head>
  
<body>
    <center>
        <h1 style="color:green;width:50%;"
            GeeksForGeeks 
        </h1>
  
        <h2>DOM getAttribute() Method </h2>
        <br>
        <button id="button" onclick="geeks()">
            Submit
        </button>
        <p id="gfg"></p>
  
        <script>
            function geeks() {
                var rk =
                    document.getElementById("button").getAttribute("onClick");
                    document.getElementById("gfg").innerHTML = rk;
            }
        </script>
  
    </center>
</body>
  
</html>

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>DOM getAttribute() Method </title>
</head>
  
<body>
    <center>
        <h1 style="color:green;width:50%;"
                GeeksForGeeks 
            </h1>
        <h2>DOM getAttribute() Method </h2>
        <a id="gfg" href="www.geeksforgeeks.com">
          Geeksforgeeks
        </a>
        <br>
        <br>
        <button id="button" onclick="geeks()">
            Submit
        </button>
        <br>
        <p id="rk"></p>
  
        <script>
            function geeks() {
                var rk =
                    document.getElementById("gfg").getAttribute("href");
                    document.getElementById("rk").innerHTML = rk;
            }
        </script>
  
    </center>
</body>
  
</html>

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

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

  • Google Chrome 15.0
  • Internet Explorer 11.0
  • Firefox 15.0
  • Opera 15.0
  • Safari 6.0


My Personal Notes arrow_drop_up


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.