Last active
April 7, 2022 20:09
-
-
Save Daniel-Hug/1415b4d027e3e9854456f4e812ea2ce1 to your computer and use it in GitHub Desktop.
Get the text nodes which are descendants of the passed parent
This file contains hidden or 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
// 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; | |
} |
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
or