What is the removeAttributeNode() DOM method?
Overview
In this shot we will learn how to remove any attribute in an element using the DOM method removeAttributeNode().
The method removeAttributeNode() is applied on the element from which we want to remove an attribute. A parameter is passed, which is the attribute node that we want to remove from the element.
Code
Let’s write a code to remove a CSS class attribute from an element.
Explanation
- Line 5: We have a CSS class with only one property
color. - Line 12: We define a
pelement with idelemand classtest. - Line 14: A paragraph element to display text.
- Line 16: We have written a button tag with the function
change(). It will be called when the user clicks on it. - Line 20: We use
getElementById()to get paragraph element with idelemand assign it to a variable. - Line 21: We apply
getAttributeNode()on the variable, and pass the attribute name in it. This attribute will be removed from the elementp. - Line 22: On the variable
elem, which has the element from which we will remove the atrribute, we applyremoveAttributeNode()and pass the attribute nodeattr, which we got in an earlier step. This will remove the attribute from the element.
Output
We see that before clicking the button, the color of the text was red due to the CSS class test, but when we click the button, the class attribute from the element is removed and the text color is changed to the default color black.