Created
August 26, 2014 23:42
-
-
Save htmlr/a5f1d9cc6f1212f8f733 to your computer and use it in GitHub Desktop.
Getting text nodes using Javascript
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
| // source: http://stackoverflow.com/a/4399718/417933 | |
| function getTextNodesIn(node, includeWhitespaceNodes) { | |
| var textNodes = [], nonWhitespaceMatcher = /\S/; | |
| function getTextNodes(node) { | |
| if (node.nodeType == 3) { | |
| if (includeWhitespaceNodes || nonWhitespaceMatcher.test(node.nodeValue)) { | |
| textNodes.push(node); | |
| } | |
| } else { | |
| for (var i = 0, len = node.childNodes.length; i < len; ++i) { | |
| getTextNodes(node.childNodes[i]); | |
| } | |
| } | |
| } | |
| getTextNodes(node); | |
| return textNodes; | |
| } | |
| getTextNodesIn(el); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment