gettextnodesbyvalue_function1.htm ( 7,481 Bytes )
GetTExtNodesByValue() searches through the document object model (DOM) looking for text nodes that contain the given text string. All that is required is the target string argument "strText". As an option, you can pass a root DOM node, "objNode," to start with. If you do not provide this node, the function will default to the BODY tag and search everything within it. Currently, the search is case insensitive. It will match uppercase and lowercase versions of the target string. The method will return an array of nodes. If no matching text nodes were found, the method will return an empty array.
One of the really interesting things about this method is its use of the sub-function, SearchNodeForText(), which is defined within the main function and called recursively. Because this is a sub-function, it has both a local scope and access to its parent's scope. Since this is somewhat new for me, I am not using best practices for passing arguments to the sub-function. For instance, I pass the array of text nodes, but not the search text. From a practical standpoint, neither is required since they are available in the parent's scope; however, passing or not, they should be consistent.
Note: Since I first posted this, I have found some bugs and some things that were not 100% cross-browser compatible. I have since updated the code below and the attached demo. It should now work in Win IE, FireFox and Mac IE, Safari, and FireFox.
Added May 10, 2006 / Updated May 11, 2006