Skip to content

Instantly share code, notes, and snippets.

@timc1
Created January 26, 2019 21:55
Show Gist options
  • Save timc1/c796371bb6a152c1ca4c634bb9b8ade5 to your computer and use it in GitHub Desktop.
Save timc1/c796371bb6a152c1ca4c634bb9b8ade5 to your computer and use it in GitHub Desktop.
removes all attributes from a dom node and its children
// recursively remove all attributes from a node and all its children
const recursiveRemove = node => {
removeAttributes(node)
while (node.childNodes.length > 0) {
for (let child of node.childNodes) {
node = child
if (node.nodeName !== 'IMG') {
recursiveRemove(child)
}
}
}
}
// remove all attributes from a dom node
const removeAttributes = node => {
const attributes = node.attributes
if (attributes) {
Object.entries(attributes).forEach(attr => {
const name = attr[1].nodeName
node.removeAttribute(name)
})
}
}
const node = document.querySelector(document.body)
recursiveRemove(node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment