
- 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 removeAttribute() Method
The HTML DOM Element removeAttribute() method is used to remove any attribute that has been set on an HTML element within the DOM structure.
The removeAttribute() method is similar to the removeAttributeNode() method, but the key difference is that the removeAttribute() directly removes an attribute by its name, while the removeAttributeNode() requires an attribute node object as a parameter to remove it.
Syntax
Following is the syntax of the HTML DOM Element removeAttribute() method −
element.removeAttribute(attribute_name)
Parameters
This method accepts a single parameter as mentioned below:
Parameter | Description |
---|---|
attribute_name | A string that contains the name of the attribute to be removed. |
Return Value
This method does not return any value.
Example 1: Removing class Attribute from p Element
The following program demonstrates the usage of the HTML DOM element removeAttribute() method. It removes the class attribute from the <p> element.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element removeAttribute()</title> <style> .highlight { color: green; font-weight: bolder; font-size: 25px; } </style> </head> <body> <h3>HTML DOM Element removeAttribute() Method</h3> <p>Click the button to remove a class attribute!!</p> <p id="myPara" class="highlight">This paragraph has a class attribute.</p> <button onclick="removeClassAttribute()">Remove Class Attribute</button> <div id="ot"></div> <script> function removeClassAttribute() { const p=document.getElementById('myPara'); p.removeAttribute('class'); document.getElementById('ot').textContent = 'Class attribute removed!'; } </script> </body> </html>
The above program removes the "class" attribute from the "p" element.
Example 2: Removing href Attribute from Anchor (a) Tag
Following is another example of the HTML DOM Element removeAttribute() method. We use this method to remove the "href" attribute from the anchor (<a>) tag −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element removeAttribute()</title> </head> <body> <h3>HTML DOM Element removeAttribute() Method</h3> <p>Click the button to remove a href attribute!!</p> <a href="https://www.tutorialspoint.com" id="myLink">Visit Tutorialspoint</a> <button onclick="removeClassAttribute()">Remove href Attribute</button> <div id="ot"></div> <script> function removeClassAttribute() { const p = document.getElementById('myLink'); p.removeAttribute('href'); document.getElementById('ot').textContent = 'href attribute removed!'; } </script> </body> </html>
Once the above program is executed, it removes the "href" attribute from the anchor (a) tag (element).
Example 3: Adding and Removing readonly Attribute
The example below uses two different methods removeAttribute() and setAttribute() to remove and add an attribute to an element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element removeAttribute()</title> </head> <body> <h1>HTML DOM Element removeAttribute() Method</h2> <p>Click the button to remove and add a "id" attribute!!</p> <input type="text" id = "In" value="Initial value" readonly> <button onclick="toggleReadonly()">Toggle Readonly Attribute</button> <div id="ot"></div> <script> function toggleReadonly() { const input=document.getElementById('In'); if (input.hasAttribute('readonly')) { input.removeAttribute('readonly'); document.getElementById('ot').textContent = 'Readonly attribute removed!'; } else { input.setAttribute('readonly', true); document.getElementById('ot').textContent = 'Readonly attribute added!'; } } </script> </body> </html>
The above program adds and removes the "read-only" attribute dynamically.
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
removeAttribute() | Yes | Yes | Yes | Yes | Yes |