I had to remove a HTML element on page unmount in React and I came to know about removeAttribute() method. Let’s have look at this.

Syntax

node.removeAttribute(attrName);
// node is nothing but an element whose attribute has to be removed

Return value is undefined

Parameters

A string specifying the name of the attribute to remove from the element. If the specified attribute does not exist,removeAttribute() returns without generating an error.


Example

HTML

<div id="box" style="width: 100px; height: 100px; background: green;"></div>
<!-- want to remove style attribute -->


JS

const element = document.getElementById("box");

element.removeAttribute("style");


If you find it helpfull please share with your network βœ….

Happy Coding πŸ™Œ !