Is there any way to get the collection of all textNode objects within a document?
getElementsByTagName() works great for Elements, but textNodes are not Elements.
Update: I realize this can be accomplished by walking the DOM – as many below suggest. I know how to write a DOM-walker function that looks at every node in the document. I was hoping there was some browser-native way to do it. After all it’s a little strange that I can get all the <input>s with a single built-in call, but not all textNodes.
Update:
I have outlined some basic performance tests for each of these 6 methods over 1000 runs.
getElementsByTagNameis the fastest but it does a half-assed job, as it does not select all elements, but only one particular type of tag ( i thinkp) and blindly assumes that its firstChild is a text element. It might be little flawed but its there for demonstration purpose and comparing its performance toTreeWalker. Run the tests yourselves on jsfiddle to see the results.Let’s assume for a moment that there is a method that allows you to get all
Textnodes natively. You would still have to traverse each resulting text node and callnode.nodeValueto get the actual text as you would do with any DOM Node. So the issue of performance is not with iterating through text nodes, but iterating through all nodes that are not text and checking their type. I would argue (based on the results) thatTreeWalkerperforms just as fast asgetElementsByTagName, if not faster (even with getElementsByTagName playing handicapped).Source for each method:
TreeWalker
Recursive Tree Traversal
Iterative Tree Traversal
querySelectorAll
getElementsByTagName (handicap)
XPath
Also, you might find this discussion helpful – http://bytes.com/topic/javascript/answers/153239-how-do-i-get-elements-text-node