how to remove an attribute from the element using JavaScript

published: 25 May 2022

2 min read

how to remove an attribute from the element using JavaScript

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.

how to remove an attribute from the element using JavaScript | Coding Tips And Tricks

Are we missing something?  Help us improve this article. Reach out to us.

Are you looking for other code tips?

Check out what's on in the category: javascript, programming
Check out what's on in the tag: javascript, programming