How to remove attributes from a DOM element using JavaScript?

October 8, 2020 - 1 min read

To remove an attribute from a DOM element, we can use the removeAttribute() method in the element in JavaScript.

Let's say we have a paragraph tag with id attribute like this,

<!-- Paragraph -->
<p id="myParagraph">Hello World!</p>

Now let's remove the id attribute using the removeAttribute() method in the paragraph element.

// get refernce to the paragragh element
const paragraph = document.querySelector("p");

// remove the id attribute using
// the removeAttribute() method
paragraph.removeAttribute("id");

// try to get the id attribute now
console.log(paragraph.getAttribute("id")); // null
  • The method accepts a valid tag name of string type as the parameter.

See this example live in JSBin.

Feel free to share if you found this useful 😃.