Last active
November 23, 2019 15:07
-
-
Save tylerthebuildor/c19899b5b671e2dfeb25c2c1ad498768 to your computer and use it in GitHub Desktop.
Get all text values under a specific node
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
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