Skip to content

Instantly share code, notes, and snippets.

@Melipone
Created February 20, 2011 04:08
Show Gist options
  • Save Melipone/835683 to your computer and use it in GitHub Desktop.
Save Melipone/835683 to your computer and use it in GitHub Desktop.
Week 4 homework #3 on the Theory of the DOM
<script type="text/javascript">
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function countTextNodes() {
var sum = 0;
function count (node) {
if (node.nodeType == 3) // TEXT_NODE
sum++;
}
var root = document.documentElement;
walkTheDOM(root, count);
// alert (sum + " text nodes found");
var foo = document.getElementById("foo");
foo.setAttribute('style',"font-weight:bold");
var newElement = document.createTextNode ("There are " + sum + " text nodes");
foo.appendChild(newElement);
}
</script>
<p>
<span id="foo" >
<button type="button" onclick="countTextNodes()">Count Text Nodes</button>
</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment