
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element hasAttributes() Method
The HTML DOM Element hasAttributes() method is used to determine whether an element in the HTML DOM has any attributes. An attributes allow you to display additional information about the element.
This method returns a boolean value: true if the element has one or more attributes, otherwise it returns false.
See also: The hasAttribute() method is similar to hasAttributes() method. This method checks for the existence of specific attributes, whereas the hasAttributes() method checks whether the element has any attributes at all.
Syntax
Following is the syntax of the HTML DOM Element hasAttributes() method −
element.hasAttributes();
Parameters
This function does not accept any parameter.
Return Value
This method returns a boolean value 'true' if the element has attributes; otherwise, it returns 'false'.
Example 1: Checking if Div Elements has any Attributes
The following is the basic example of the HTML DOM Element hasAttributes() method. It checks attributes for the <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element hasAttributes()</title> </head> <body> <h3>HTML DOM Element hasAttributes() Method</h3> <div id="exampleDiv" title="Sample Div">Does it has any attributes?</div> <br> <button onclick="checkAttributes()">Check Attributes</button> <p id="result"></p> <script> function checkAttributes() { const divElement = document.getElementById('exampleDiv'); let res = divElement.hasAttributes(); document.getElementById('result').textContent = "Does the div has attributes? " + res; } </script> </body> </html>
The above program displays "true", because the "div" element has "id" and "title" attributes.
Example 2: Checking Attributes for Body Element
Here is another example of the HTML DOM Element hasAttributes() method. This method is used to check whether the <body> element has any attributes −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element hasAttributes() Method</title> </head> <body> <h3>HTML DOM Element hasAttributes() Method</h3> <p>Does Body element has any attribute?</p> <button onclick="checkAttributes()">Check Body Attributes</button> <p id="result"></p> <script> function checkAttributes() { const bodyElement = document.body; let res = bodyElement.hasAttributes(); document.getElementById('result').textContent = "Does Body element has any attributes? " + res; } </script> </body> </html>
When the button is clicked, it will display "false" because the "body" has no attributes.
Example 3: Checking if an Anchor Tag has Attributes
In the example below, we use the hasAttributes() method to determine if the anchor (<a>) element has any attributes −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element hasAttributes()</title> </head> <body> <h3>HTML DOM Element hasAttributes() Method</h3> <p>Checks if Anchor tag has any Attributes</p> <a id="myLink" href="https://www.tutorialspoint.com/">Example Link</a> <button onclick="checkAttributes()">Check Attributes</button> <p id="result"></p> <script> function checkAttributes() { const linkElement = document.getElementById('myLink'); document.getElementById('result').textContent = "The link has attributes."; } </script> </body> </html>
The above program displays the message "The link has attributes." if the anchor element has the attribute.
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
hasAttributes() | Yes | Yes | Yes | Yes | Yes |