
- 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 getAttributeNode() Method
The HTML DOM ElementgetAttributeNode()method is used to get the attribute node object that represents one of the attributes of an HTML element in DOM (Document Object Model).
The returned object includes both the attribute's name and its value. By specifying the name and value property you can access them.
Syntax
Following is the syntax of the HTML DOM Element getAttributeNode() method −
element.getAttributeNode(attributeName)
Parameters
This method accepts a single parameter as listed below.
Parameters | Description |
---|---|
attributeName | The name of the attribute for which the method returns the object. |
Return Value
This method returns the name of the attribute for which the method returns the object.
Example 1: Retrieving a Specific Attribute Node
The following program demonstrates the usage of the HTML DOM Element getAttributeNode() method. It returns the node name of the <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element getAttributeNode()</title> </head> <body> <h3>HTML DOM Element getAttributeNode() method</h3> <hp>Retrieving a Specific Attribute Node</p><br> <div id="mydivElement" data-info="Accessing div element"> This is div Element</div> <p id="res">The attribute node is: </p> <script> var element = document.getElementById("mydivElement"); var attributeNode = element.getAttributeNode("data-info"); document.getElementById('res').innerHTML = 'The attribute node is: ' + attributeNode; </script> </body> </html>
The above program accesses and displays the object [object Attr].
Example 2: Retrieving class Attribute Value
Following is another example of the HTML DOM Element getAttributeNode() method. This method retrieves an attribute node object, and through that, we retrieve the class attribute value −
<!DOCTYPE html> <html lang="en"> <head> <style> .important{ color: blue; } </style> </head> <body> <h1 class = "important" >Document Information</h1> <h2>HTML DOM Element getAttributeNode() Method</h2> <p id = "demo"></p> <script> const h1Element = document.querySelector("h1"); const classAttrNode = h1Element.getAttributeNode("class"); document.getElementById("demo").textContent = "The class attribute node value: " + classAttrNode.value; </script> </body> </html>
Once the above program is executed, it displays the "class" attribute node value.
Example 3: Node Value of target Attribute
In the example below, the getAttributeNode() method retrieves the node object of the target attribute and through that retrieves the value −
<!DOCTYPE html> <html> <head> <style> .active { font-weight: bold; color: blue; } </style> </head> <body> <h1>Navigation Menu</h1> <h2>The getAttributeNode() Method</h2> <ul class = "list" id="menu"> <li><a href="#home" id="homeLink">Home</a></li> <li><a href="#about" id="aboutLink">About Us</a></li> </ul> <p>Click on a link below to display its target attribute value:</p> <p id="targetValue"></p> <script> const menuLinks = document.querySelectorAll("#menu a"); menuLinks.forEach(link => { link.addEventListener("click", function() { const targetAttrNode = link.getAttributeNode("href"); const targetValue = targetAttrNode.value; document.getElementById("targetValue").textContent = `Target attribute value: ${targetValue}`; menuLinks.forEach(l => l.classList.remove("active")); link.classList.add("active"); }); }); </script> </body> </html>
The above program displays the "target" attribute value when the user clicks the given links.
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
getAttributeNode() | Yes 5.0 | Yes 10.0 | Yes 16.0 | Yes 5.1 | Yes 10.6 |