JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to remove an attribute from an element in JavaScript?

With the help of the removeAttribute() method, you can remove any attribute associated with an HTML element. The syntax for the removeAttribute() method is

htmlElement.removeAttribute(attributeName);

For example, if you want to remove the alt attribute of an img element, then run the following code:

HTML Code

<img src="/images/javascript.png" id="logo" alt="JavaScript Logo" />

JavaScript Code

let image = document.getElementById('logo');
image.removeAttribute('alt');

You can also remove data-* attribute using the removeAttribute() method.

For example, if you want to remove the data-color attribute of an img element, then select the img element and then call removeAttribute() method and pass data-color to it.

<img src="/images/javascript.png" id="logo" data-color="yellow" alt="JavaScript Logo" />
let image = document.getElementById('logo');
image.removeAttribute('data-color');

Recommended Posts