Remove all attributes

Viewed 61831

Is it possible to remove all attributes at once using jQuery?

<img src="example.jpg" width="100" height="100">

to

<img>

I tried $('img').removeAttr('*'); with no luck. Anyone?

10 Answers

One-liner, no jQuery needed:

[...elem.attributes].forEach(attr => elem.removeAttribute(attr.name));

One-liner.

For jQuery users

$('img').removeAttr(Object.values($('img').get(0).attributes).map(attr => attr.name).join(' '));

One don't need to refer to the name of attribute to to id nowadays, since we have removeAttributeNode method.

while(elem.attributes.length > 0) {
    elem.removeAttributeNode(elem.attributes[0]);
}

Related