Posts

Showing posts from July, 2006

Getting text node recursively

This code extracts text (nodeValue) of first text node presented in DOM tree. It works with complex structure of DOM trees, but somehow it appeared to be a quite complicated code. Can anyone improve it? : /** Return text node if exists in hierarchy */ function findChildTextNode(startNode) {    if (startNode.nodeType == 3) return startNode;     var childList = startNode.childNodes;     for (var i=0; i < childList.length; i++) {         return findChildTextNode(childList[i]);     }    return null; } /** Returns text node recursively */ function findTextNode(inerestNode) {     var foundTextNode = null;     var tags = inerestNode.getElementsByTagName("*");     for (var i = 0; i < tags.length; i++) {         foundTextNode = findChildTextNode(tags[i]);         if (foundTextNode) return foundTextNode;     }     return null; } var interestTextNode = findTextNode(interestNode);