
- 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 attributes Property
The HTML DOM attributes property provides you access to a collection of attributes belonging to an HTML element. This collection is returned as a NamedNodeMap, which allows you to interact with the attributes using their names and retrieve their values.
A NamedNodeMap is an object that holds a collection of an element's attributes. It allows us to access attributes indexed by their names and supports various operations.
Syntax
Following is the syntax of the HTML DOM attributes property −
node.attributes
Parameters
Since it is a property, it does not accept any parameters.
Return Value
This property returns 'NameNodeMap' containing the collection of attributes of an HTML element and It returns 'null' if the elements has no attributes
Example 1: Modifying Attributes of the p element
The following example demonstrates the usage of the HTML DOM attributes property. It modifies the content of the <p> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM attributes</title> <style> .highlight { background-color: yellow; } </style> </head> <body> <p></p>Modifies attributes of the P Element</p> <p id="myParagraph">Example Text</p> <button onclick="modifyParagraph()">Modify</button> <script> function modifyParagraph() { var paragraph = document.getElementById("myParagraph"); paragraph.setAttribute("title", "Modified"); paragraph.textContent = "Modified Text"; paragraph.classList.add("highlight"); } </script> </body> </html>
Example 2: Checking for Attribute Existence
Following is another example of the HTML DOM attributes property. This property is used to check whether the "title" attribute exists on a paragraph element −
<!DOCTYPE html> <html> <head> <title>HTML DOM Attributes</title> </head> <body> </p>Checking for attribute Existence</p> <p id="myParagraph" title="Example Title">Example Text</p> <button onclick="checkAttributeExistence()">Check Attribute</button> <script> function checkAttributeExistence() { var paragraph = document.getElementById("myParagraph"); var hasTitle = paragraph.hasAttribute("title"); alert("Title Attribute Exists: " + hasTitle); } </script> </body> </html>
Example 3: Retrieves Attribute List from HTML Element
The example below retrieves and displays all attributes of an HTML <div> element. All the attributes will be displayed in list format when the button is clicked −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM attributes</title> </head> <body> <div id="ex-Div"class="example" data-info="ex-data"> Hit the button to see the Div Elements... </div> <button onclick="showAttributes()">Show Attributes</button> <ul id="attributeList"></ul> <script> function showAttributes() { const element = document.getElementById('ex-Div'); const attributeList = document.getElementById('attributeList'); attributeList.innerHTML = ''; for (let attr of element.attributes) { const li = document.createElement('li'); li.textContent = `${attr.name}: ${attr.value}`; attributeList.appendChild(li); } } </script> </body> </html>
Example 4: Getting Specific Attribute Value
This example retrieves the value of the title attribute from a paragraph element with the ID "myParagraph" and displays it in an alert −
<!DOCTYPE html> <html> <head> <title>HTML DOM attributes</title> </head> <body> <p>Getting Specific Attribute</p> <p id="myParagraph" title="Example Title">Example Text</p> <button onclick="getAttributeValue()">Get Attribute Value</button> <script> function getAttributeValue() { var paragraph = document.getElementById("myParagraph"); var titleValue =paragraph.getAttribute("title"); alert("Title Attribute Value: " + titleValue); } </script> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
attributes | Yes | Yes | Yes | Yes | Yes |