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 9160171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:40:26+00:00 2026-06-17T13:40:26+00:00

I am performing some simple JS test for traversing and i was wondering why

  • 0

I am performing some simple JS test for traversing and i was wondering why my documentElement consist of one null object?

<html>
    <head>
        <title>My document</title>
    </head>
    <body>
        <h1>Some header</h1>
        <p id="pID">Some paragraph</p>
        <p name="pNAME">Another paragraph/p>
    </body>
</html>
<script type="text/javascript">
    var rootElement = document.documentElement;
    var childNodes = rootElement.childNodes;

    for (var i = 0; i < childNodes.length; i++) {
      document.write(childNodes[i].localName);
      document.write("<br>");
    }
</script>

It returns head, null, body. But why is there 3 childNodes in the rootElement?

  • 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-06-17T13:40:27+00:00Added an answer on June 17, 2026 at 1:40 pm

    If you list only the objects instead of their localName you will see:

    [object HTMLHeadElement]
    [object Text]
    [object HTMLBodyElement]
    

    So you receive null for the localName of the text node. To fix this you need children instead of childNodes:

    <script type="text/javascript">
        var rootElement = document.documentElement;
        var childNodes = rootElement.children;
    
        for (var i = 0; i < childNodes.length; i++) {
          document.write(childNodes[i]);
          document.write("<br>");
        }
    </script>
    

    Here is demo in JSFiddle.

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

Sidebar

Related Questions

I have a web-site that generates some simple tabular data as html tables, many
I am performing a simple multiplication with BigDecimal and I have found some strange
I have some simple GWT client code that I would like to test using
I am doing some simple sanity validation on various types. The current test I'm
While performing some upcoming maintenance, I'm going to have to redirect all site traffic
I'm performing some parameter validation in a method and throwing an exception where necessary.
We are performing some JMeter tests on a JBoss 4.0.5 deployed web app. We
In my android app I am performing some operations in the doInBackground by extending
I have a WCF service that is performing some updates across a couple of
I have my php app working fine, performing some other Graph API functions. However,

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.