Files
30-seconds-of-code/snippets/remove-attributes.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

684 B

title, tags, cover, author, firstSeen
title tags cover author firstSeen
Remove attributes browser new-york chalarangelo 2022-07-20T05:00:00-04:00

Removes all attributes from an HTML element.

  • Use Element.attributes and Object.values() to get all the attributes of the element.
  • Use Array.prototype.forEach() and object destructuring to get the name of each attribute and Element.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