HTML | DOM hasAttributes() Method

The DOM hasAttribute() method is a boolean function which returns either true or false but not both. This method returns a true value if the element contains an attribute otherwise, it returns false. It is very useful to know whether the element in the document has attributes or not.

Syntax:

node.hasAttributes()

Parameters: It does not contains any parameter.

Return Value: It return true if the element contains attribute otherwise it returns false.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>
            DOM hasAttributes() Method
        </title
    </head
      
    <body
        <center
            <h1 style = "color:green;width:50%;" id = "sudo"
                GeeksForGeeks 
            </h1
              
            <h2>DOM hasAttributes() Method </h2>     
              
            <p>Click on the button to check if that body 
            element has any attributes</p>
              
            <button type = "button" onclick = "geeks()"
                Submit 
            </button
              
            <script
                function geeks() { 
                    var s = document.body.hasAttributes();
                    document.getElementById('gfg').innerHTML = s;
                
            </script
              
            <p id = "gfg"></p>
        </center
    </body
</html>                                

chevron_right


Output:
Before click on the Button:

After click on the Button:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head id = "rk"
        <title>
            DOM hasAttributes() Method
        </title
    </head
      
    <body
        <center
            <h1 style = "color:green;width:50%;" id = "sudo"
                GeeksForGeeks 
            </h1
              
            <h2>DOM hasAttributes() Method </h2>
              
            <p>Click on the button to check if that
            head element has any attributes</p>     
  
            <button type = "button" onclick = "geeks()"
                Submit 
            </button
              
            <script
                function geeks() { 
                    var s = document.head.hasAttributes();
                    document.getElementById('gfg').innerHTML = s;
                
            </script
                <p id = "gfg"></p>
        </center
    </body
</html>                    

chevron_right


Output:
Before click on the Button:

After click on the Button:

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

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


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.