Remove attributes
JavaScript, Browser · Jul 20, 2022

Removes all attributes from an HTML element.
- Use
Element.attributes
andObject.values()
to get all the attributes of the element. - Use
Array.prototype.forEach()
and object destructuring to get the name of each attribute andElement.removeAttribute()
to remove it from the element.
const removeAttributes = element =>
Object.values(element.attributes).forEach(({ name }) =>
element.removeAttribute(name)
);
removeAttributes(document.querySelector('p.special'));
// The paragraph will not have the 'special' class anymore
Written by Angelos Chalaris
I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.
If you want to keep in touch, follow me on GitHub.