Generally, when moving the DOM, you want to specify a starting point. From there, check to see if the starting point has childNodes . If so, skip them and restart the function if they also have childNodes .
Here is the code that is output to the console using the DOM form of these nodes (I used the document / HTML element as the starting point). You will need to run if if against window.console , if you allow non-developers to load this page / code and use console :
recurseDomChildren(document.documentElement, true); function recurseDomChildren(start, output) { var nodes; if(start.childNodes) { nodes = start.childNodes; loopNodeChildren(nodes, output); } } function loopNodeChildren(nodes, output) { var node; for(var i=0;i<nodes.length;i++) { node = nodes[i]; if(output) { outputNode(node); } if(node.childNodes) { recurseDomChildren(node, output); } } } function outputNode(node) { var whitespace = /^\s+$/g; if(node.nodeType === 1) { console.log("element: " + node.tagName); }else if(node.nodeType === 3) {
Example: http://jsfiddle.net/ee5X6/
user1385191
source share