Skip to content

Commit

Permalink
Add removeAttributes snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Jul 16, 2022
1 parent 674bd91 commit 69a116d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions snippets/removeAttributes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Remove attributes
tags: browser
expertise: beginner
cover: blog_images/new-york.jpg
author: chalarangelo
firstSeen: 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.

```js
const removeAttributes = element =>
Object.values(element.attributes).forEach(({ name }) =>
element.removeAttribute(name)
);
```

```js
removeAttributes(document.querySelector('p.special'));
// The paragraph will not have the 'special' class anymore
```

0 comments on commit 69a116d

Please sign in to comment.