
- 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 hasAttribute() Method
The HTML DOM Element hasAttribute() method is used to check whether the specified attribute present within an HTML element. An attribute allow you display an additional information about the particular element.
This method returns a boolean value true if the attribute exists , otherwise it returns false.
See also:- The hasAttributes() method is similar to the hasAttribute() method, but it checks for the presence of any attributes in an element, whereas the hasAttribute() method checks for the presence of a specific attribute within the element.
Syntax
Following is the syntax of the HTML DOM Element hasAttribute() method −
element.hasAttribute(name);
Parameters
This method accepts a single parameter as mentioned below −
Parameter | Description |
---|---|
name | A string representing the name of the attribute. |
Return Value
The method returns true if the attribute is present on the HTML element and false otherwise.
Example 1: Checking for a Specific Attribute
If the specified attribute is present within the element, the HTML DOM Element hasAttribute() method will return true −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element hasAttribute()</title> </head> <body> <h3>HTML DOM Element hasAttribute() Method</h3> <div id="exampleDiv" title="Sample Div"></div> <p id="attributeInfo"></p> <script> const divElement = document.getElementById('exampleDiv'); const hasTitle = divElement.hasAttribute('title'); document.getElementById('attributeInfo').textContent = "Does the div have a title attribute? " + hasTitle.toString(); </script> </body> </html>
The above program returns "true".
Example 2
If the specified attribute is not present within the element, the HTML DOM Element hasAttribute() method will return false −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element hasAttribute()</title> </head> <body> <h3>HTML DOM Element hasAttribute() Method</h3> <div id="exampleDiv" title="Sample Div"></div> <p id="attributeInfo"></p> <script> const divElement = document.getElementById('exampleDiv'); const hasTitle = divElement.hasAttribute('class'); document.getElementById('attributeInfo').textContent = "Does the div have a 'class' attribute? " + hasTitle.toString(); </script> </body> </html>
Since the specified attribute does not exist within the element, the above program will return "false".
Example 3: Checking for multiple Attribute
In the example below, we use the hasAttribute() method to check whether the <div> element has the id and data-info attributes −
<!DOCTYPE html> <html> <head> <title>HTML DOM Element hasAttribute()</title> </head> <body> <h3>HTML DOM Element hasAttribute() Method</h3> <div id="exampleDiv" title="Div" data-info="12345"> Hello! I am Div. Check my attributes? Hit the button! </div> <br> <button onclick="displayAttributes()">Display Attributes</button> <script> function displayAttributes() { const divElement = document.getElementById('exampleDiv'); const hasTitle = divElement.hasAttribute('title'); const hasDataInfo = divElement.hasAttribute('data-info'); alert(`Title attribute: ${hasTitle} \nData-info attribute: ${hasDataInfo}`); } </script> </body> </html>
When the button is clicked, an alert message will pop up displaying the "title" and "data-info" attributes, along with a boolean value indicating their presence.
Example 4: Handling absence of an Attribute
Here is another example of the hasAttribute() method. We use the result of this method in a conditional statement to manage cases where an attribute does not exist −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element hasAttribute()</title> </head> <body> <h3>HTML DOM Element hasAttribute() Method</h3> <div id="exampleDiv">Does Div have any attributes?</div><br> <button onclick="displayAttributes()">Check Attributes</button> <p id="attributeInfo"></p> <script> function displayAttributes() { const divElement = document.getElementById('exampleDiv'); const hasTitle = divElement.hasAttribute('title'); document.getElementById('attributeInfo').textContent = `false - It has ${hasTitle ? 'title' : 'no'} attribute.`; } </script> </body> </html>
The above program checks the presence of an attribute within the element and displays the message accordingly.
Supported Browsers
Method | ![]() | ![]() | ![]() | ![]() | ![]() |
---|---|---|---|---|---|
hasAttribute() | Yes | Yes | Yes | Yes | Yes |