Created
January 26, 2019 21:55
-
-
Save timc1/c796371bb6a152c1ca4c634bb9b8ade5 to your computer and use it in GitHub Desktop.
removes all attributes from a dom node and its children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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