Remove all attributes of an element
Removing all attributes of an element in Vanilla JavaScript:
Removing class, id and style
HTML code:
<div class="example">
<p class="class" id="id" style="font-weight: bold;">
Removing class, id and style
</p>
</div>
JavaScript code:
<script>
var element = document.querySelector('.example p');
while( element.attributes.length > 0 ) {
element.removeAttribute( element.attributes[0].name );
}
</script>
CSS code:
.class {color: #c00;}
#id {text-transform: uppercase;}
Links
- Remove all attributes: stackoverflow.com/questions/1870441/remove-all-attributes
- Element: attributes property: developer.mozilla.org/en-US/docs/Web/API/Element/attributes
Browser support
- Windows
- Edge 12.0+
- Firefox 1.0+
- Google Chrome 1.0+
- Opera 8.0+
- Safari 3.1+
- Linux
- Firefox 1.0+
- Google Chrome 1.0+
- Opera 8.0+
- iOS
- Safari 1.0+
- Android
- Samsung Internet 1.0+
- Chrome 18.0+
- Firefox 4.0+
- Opera 10.1+