Skip to content

Instantly share code, notes, and snippets.

@tylerthebuildor
Last active November 23, 2019 15:07
Show Gist options
  • Save tylerthebuildor/c19899b5b671e2dfeb25c2c1ad498768 to your computer and use it in GitHub Desktop.
Save tylerthebuildor/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