Skip to content

Instantly share code, notes, and snippets.

@arestov
Created July 22, 2010 19:13
Show Gist options
  • Save arestov/486438 to your computer and use it in GitHub Desktop.
Save arestov/486438 to your computer and use it in GitHub Desktop.
function allChildren(root) {
var node = root.firstChild;
var result = [];
var next;
while (node && node !== root) {
result.push(node);
next = node.firstChild || node.nextSibling;
if (next) {
node = next;
} else {
while (node.parentNode && !node.nextSibling) {
node = node.parentNode;
}
node = node.nextSibling;
}
}
return result;
}
window.addEventListener("load", function(){
console.info(allChildren(document.body));
}, true);
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div>1</div>
<span>2</span>
3
<p><em><strong><code>4</code></strong></em></p>
<abbr>5</abbr>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment