Skip to content
Related Articles

Related Articles

HTML | DOM hasAttributes() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 10 Aug, 2022

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: 

HTML




<!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>

Output: 

Before click on the Button:

  

After click on the Button:

  

Example 2: 

HTML




<!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>

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 1 and above
  • Edge 12 and above
  • Internet Explorer 8 and above
  • Firefox 1 and above
  • Opera 12.1 and above
  • Safari 1 and above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!