Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:57:54+00:00 2026-05-14T04:57:54+00:00

Is there any way to get the collection of all textNode objects within a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:57:54+00:00Added an answer on May 14, 2026 at 4:57 am

    Update:

    I have outlined some basic performance tests for each of these 6 methods over 1000 runs. getElementsByTagName is the fastest but it does a half-assed job, as it does not select all elements, but only one particular type of tag ( i think p) 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 to TreeWalker. Run the tests yourselves on jsfiddle to see the results.

    1. Using a TreeWalker
    2. Custom Iterative Traversal
    3. Custom Recursive Traversal
    4. Xpath query
    5. querySelectorAll
    6. getElementsByTagName

    Let’s assume for a moment that there is a method that allows you to get all Text nodes natively. You would still have to traverse each resulting text node and call node.nodeValue to 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) that TreeWalker performs just as fast as getElementsByTagName, if not faster (even with getElementsByTagName playing handicapped).

    Ran each test 1000 times.
    
    Method                  Total ms        Average ms
    --------------------------------------------------
    document.TreeWalker          301            0.301
    Iterative Traverser          769            0.769
    Recursive Traverser         7352            7.352
    XPath query                 1849            1.849
    querySelectorAll            1725            1.725
    getElementsByTagName         212            0.212
    

    Source for each method:

    TreeWalker

    function nativeTreeWalker() {
        var walker = document.createTreeWalker(
            document.body, 
            NodeFilter.SHOW_TEXT, 
            null, 
            false
        );
    
        var node;
        var textNodes = [];
    
        while(node = walker.nextNode()) {
            textNodes.push(node.nodeValue);
        }
    }
    

    Recursive Tree Traversal

    function customRecursiveTreeWalker() {
        var result = [];
    
        (function findTextNodes(current) {
            for(var i = 0; i < current.childNodes.length; i++) {
                var child = current.childNodes[i];
                if(child.nodeType == 3) {
                    result.push(child.nodeValue);
                }
                else {
                    findTextNodes(child);
                }
            }
        })(document.body);
    }
    

    Iterative Tree Traversal

    function customIterativeTreeWalker() {
        var result = [];
        var root = document.body;
    
        var node = root.childNodes[0];
        while(node != null) {
            if(node.nodeType == 3) { /* Fixed a bug here. Thanks @theazureshadow */
                result.push(node.nodeValue);
            }
    
            if(node.hasChildNodes()) {
                node = node.firstChild;
            }
            else {
                while(node.nextSibling == null && node != root) {
                    node = node.parentNode;
                }
                node = node.nextSibling;
            }
        }
    }
    

    querySelectorAll

    function nativeSelector() {
        var elements = document.querySelectorAll("body, body *"); /* Fixed a bug here. Thanks @theazureshadow */
        var results = [];
        var child;
        for(var i = 0; i < elements.length; i++) {
            child = elements[i].childNodes[0];
            if(elements[i].hasChildNodes() && child.nodeType == 3) {
                results.push(child.nodeValue);
            }
        }
    }
    

    getElementsByTagName (handicap)

    function getElementsByTagName() {
        var elements = document.getElementsByTagName("p");
        var results = [];
        for(var i = 0; i < elements.length; i++) {
            results.push(elements[i].childNodes[0].nodeValue);
        }
    }
    

    XPath

    function xpathSelector() {
        var xpathResult = document.evaluate(
            "//*/text()", 
            document, 
            null, 
            XPathResult.ORDERED_NODE_ITERATOR_TYPE, 
            null
        );
    
        var results = [], res;
        while(res = xpathResult.iterateNext()) {
            results.push(res.nodeValue);  /* Fixed a bug here. Thanks @theazureshadow */
        }
    }
    

    Also, you might find this discussion helpful – http://bytes.com/topic/javascript/answers/153239-how-do-i-get-elements-text-node

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to get the effect of running python -u from within
Is there any non-awful way to have a collection of objects of more than
Is there any easy way to query a heterogeneous collection, where the objects in
Is there any way to get the previous hash using the history object ?I
Is there any way to get direct URL to a JSF bean action method?
Is there any way to get how much memory a service application is using
Is there any way to get the previously associated Wifi networks for an Android
Is there any way to get the location of a cell phone (i.e. latitude/longitude)
Is there any way I get get the size of an NSWindow (in pixels)
Is there any way to get the local end point of a connected Silverlight

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.