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

  • Home
  • SEARCH
  • 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 4327550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:33:18+00:00 2026-05-21T09:33:18+00:00

Let say I have a mark up like this <html id=test> <body> Some text

  • 0

Let say I have a mark up like this

<html id="test">
<body>
Some text node.
<div class="cool"><span class="try">This is another text node.</span></div>
Yet another test node.
</body>
</html>

my js code

function countText(node){
 var counter = 0;
 if(node.nodeType === 3){
     counter+=node.nodeValue.length;
     countText(node);
 }
 else{}
}

Now if I want to count the text nodes

console.log("count text : " + countText(document.getElementById("test"));

this should return me the count but its not working and moreover what should I put in else condition.
I never used nodeType so kind of having problem using it . Any help will be appreciated.

  • 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-21T09:33:18+00:00Added an answer on May 21, 2026 at 9:33 am

    There are a couple of things wrong in your code:

    • Your HTML is malformed.
    • You are appending text to your counter instead of increasing it.
    • You never loop over the children of the a node, you always pass the same node to the recursive call.
    • You don’t do anything if a node is not a text node.

    This will work:

    function countText(node){
        var counter = 0;
        if(node.nodeType === 3){
            counter++;
        }
        else if(node.nodeType === 1) { // if it is an element node, 
           var children = node.childNodes;    // examine the children
           for(var i = children.length; i--; ) {
              counter += countText(children[i]);
           }
        }
        return counter;  
    }
    
    alert(countText(document.body));
    

    DEMO

    Which number corresponds to which node type can be found here.


    Update:

    If you want to count the words, you have to split each text node into words first. In the following I assume that words are separated by white spaces:

    if(node.nodeType === 3){
        counter = node.nodeValue.split(/\s+/g).length;
    }
    

    Update 2

    I know you want to use a recursive function, but if you want to count the words only, then there is a much easier and more efficient way:

    function countWords(node){
        // gets the text of the node and all its descendants
        var text = node.innerText || node.textContent
        return text.split(/\s+/g).length;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have some code (using CherryPy) that looks like this: import cherrypy
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Let say that I have a website with some information that could be access
Let's say you have a class called Customer, which contains the following fields: UserName
Let say I have a sheet in with columns Customer and CreatedDate with lots
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I have a web page that currently accepts a single ID value
Let's say I have the following simple enum: enum Response { Yes = 1,
Let's say I have the following simple table variable: declare @databases table ( DatabaseID

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.