To remove an attribute from an HTML element, you can use the removeAttribute()
method. This method removes the specified attribute from the element.
Let us say that you have got the following anchor link:
<a href="http://example.com" title="Go Back" data-role="self">Click Me</a>
Now you want to remove the title
attribute from the above <a>
tag.
You can use the following code snippet:
const anchor = document.querySelector('a');
anchor.removeAttribute('title');
The removeAttribute()
also works for HTML5 data-*
attributes. For example, to remove the data-role
attribute from the anchor link, you can use the following code:
const anchor = document.querySelector('a');
anchor.removeAttribute('data-role');
The removeAttribute()
method works in all modern browsers, and IE8 and above.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.