Last active
August 29, 2015 14:27
-
-
Save ernestlv/b5b286439daf3d8a1655 to your computer and use it in GitHub Desktop.
script walks The DOM from any node and prints any text content
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
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