Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Last active April 7, 2022 20:09
Show Gist options
  • Save Daniel-Hug/1415b4d027e3e9854456f4e812ea2ce1 to your computer and use it in GitHub Desktop.
Save Daniel-Hug/1415b4d027e3e9854456f4e812ea2ce1 to your computer and use it in GitHub Desktop.
Get the text nodes which are descendants of the passed parent
// Get the text nodes which are descendants of the passed parent
function getTextNodes(parent){
var all = [];
for (parent = parent.firstChild; parent; parent = parent.nextSibling) {
if (['SCRIPT','STYLE'].indexOf(parent.tagName) >= 0) continue;
if (parent.nodeType === Node.TEXT_NODE) all.push(parent);
else all = all.concat(getTextNodes(parent));
}
return all;
}
@vinaypai
Copy link

Note that includes() is not supported by any version of Internet Explorer. It's only supported in Edge 14+.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment