
- 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 getAttribute() Method
The HTML DOM getAttribute() method is used to get the value of a specified attribute that belongs to an HTML element. If the given attribute does not exist the value returned will be "null".
Syntax
Following is the syntax of the HTML DOM getAttribute() method −
element.getAttribute(attributeName)
Parameters
This method accepts a single parameter as mentioned below −
Parameter | Description |
---|---|
attributeName | The name of the attribute whose value you want to retrieve. |
Return Value
This method returns a string that holds the value of the specified attribute. If the attribute does not exists, it returns 'null'.
Example 1: Accessing and Displaying the "href" Attribute
The following is the basic example of the HTML DOM getAttribute() method. It gets the href attribute from anchor (<a>) element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getAttribute()</title> </head> <body> <h4>Retrieves the value of href attribute</h4> <a id="myLink" href="https://www.tutorialspoint.com">Example Link</a> <div id="output"></div> <script> // Selecting the anchor element by ID const myLink = document.getElementById('myLink'); const hrefVal = myLink.getAttribute('href'); // Displaying the result const od= document.getElementById('output'); od.textContent = `Link URL: ${hrefVal}`; </script> </body> </html>
Example 2: Get the Attribute Values of an HTML element
Following is another example of the HTML DOM getAttribute() method. We use this method to get all attributes, that are available inside the <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getAttribute()</title> </head> <body> <div id="myElement" class="box" data-info="example data"> <p>This is a paragraph inside a div element.</p> <a href="https://www.tutorialspoint.com">Example Link</a> </div> <div id="output"></div> <script> // Selecting the element by ID const my= document.getElementById('myElement'); //Retrieving attribute values const cv = my.getAttribute('class'); const dInv = my.getAttribute('data-info'); const hv = my.querySelector('a').getAttribute('href'); // Displaying the results const od = document.getElementById('output'); od.innerHTML = ` <p>Class attribute value: ${cv}</p> <p>Data-info attribute value: ${dInv}</p> <p>Anchor href attribute value: ${hv}</p> `; </script> </body> </html>
Example 3: Getting the Button Attribute
This example shows the usage of the DOM getAttribute() method to access and display the value of the onclick attribute from an HTML<button>element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM getAttribute()</title> </head> <body> <h4>Retrieves the value of Button attribute</h4> <button id="myB" onclick="displayMessage()">Click me</button> <div id="ot"></div> <script> function displayMessage() { const ocv = document.getElementById ('myB').getAttribute('onclick'); document.getElementById('ot').textContent = `Value of onclick attribute: ${ocv}`; } </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
getAttribute() | Yes | Yes | Yes | Yes | Yes |