Skip to content

Instantly share code, notes, and snippets.

@htmlr
Created August 26, 2014 23:42
Show Gist options
  • Select an option

  • Save htmlr/a5f1d9cc6f1212f8f733 to your computer and use it in GitHub Desktop.

Select an option

Save htmlr/a5f1d9cc6f1212f8f733 to your computer and use it in GitHub Desktop.
Getting text nodes using Javascript
// 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