Skip to content

Instantly share code, notes, and snippets.

@tylerbuchea
Last active November 23, 2019 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerbuchea/c19899b5b671e2dfeb25c2c1ad498768 to your computer and use it in GitHub Desktop.
Save tylerbuchea/c19899b5b671e2dfeb25c2c1ad498768 to your computer and use it in GitHub Desktop.
Get all text values under a specific node
function textNodesUnder(node){
const all = [];
for (node = node.firstChild; node; node = node.nextSibling){
if (node.nodeType === 3) all.push(node);
else all = all.concat(textNodesUnder(node));
}
return all;
}
let selector = '';
textNodesUnder(selector)
.map(n => n.nodeValue)
.filter(s => s.replace(/\s/g, '').length)
.join('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment