Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active August 29, 2015 14:27
Show Gist options
  • Save ernestlv/b5b286439daf3d8a1655 to your computer and use it in GitHub Desktop.
Save ernestlv/b5b286439daf3d8a1655 to your computer and use it in GitHub Desktop.
script walks The DOM from any node and prints any text content
function walkDOM(node, cb){
cb(node);
node = node.firstChild;
while(node){
walkDOM(node, cb);
node = node.nextSibling;
}
}
function walkUp(node, cb){
var parent = node.parentElement;
while (parent){
node = parent;
parent = node.parentElement;
}
walkDOM(node, cb)
}
walkUp(document.getElementsByTagName("p")[1], function(node){
if (node.nodeType === 3 && node.textContent.replace(/^\s+|\s+$/, "")){
console.log(node.textContent);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment