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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:03:03+00:00 2026-06-17T03:03:03+00:00

I am new to JS and trying to learn some basic stuff. I have

  • 0

I am new to JS and trying to learn some basic stuff. I have spent hours on this but i don’t think it should be that difficult. For some reason, my computer is not recognizing that I am asking for childNodes.

This is a simply script that is only trying to count the number of li tags I have. I know there are other ways to do this but i am trying to learn this way.

<title>To-Do List</title>
<script>
    function findComments(){
        var bodyTag = document.getElementsByTagName("ol");
        var count = 0;
        for(var i=0;i<bodyTag.childNodes.length;i++){
            if(bodyTag.childNodes[i].nodeType == 1){
                count++;
            }
        }
        alert(count);       
    }
    window.onload = findComments;
</script>

<!--List is declared-->
<ol id="toDoList">
    <!--List Items are created-->
    <li>Mow the lawn</li>
    <li>Clean the windows</li>
    <li>Answer your email</li>
</ol>
<!--Main paragraph-->
<p id="toDoNotes">Make sure all these are completed by 8pm so you can watch the game on TV!</p>
    <script>
</script>

  • 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-17T03:03:03+00:00Added an answer on June 17, 2026 at 3:03 am

    getElementsByTagName returns an array, you need to retrieve its first element (and change the name from bodyTag to olTag or something since it’s not the body tag and confused the heck out of me trying to make sense of your code)

    function findComments(){
        var ol = document.getElementsByTagName("ol")[0];
        var count = 0;
        for(var i=0;i<ol.childNodes.length;i++){
            if(ol.childNodes[i].nodeType == 1){
                count++;
            }
        }
        alert(count);       
    }
    

    And here’s what you really should do now that you know what’s wrong with your code

    var ol = document.getElementsByTagName("ol")[0];
    var liCount = ol.getElementsByTagName("li").length;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

new to jquery here but I'm trying to learn some different stuff that's been
Ok, this may have been answered, but I'm new and trying to learn, so
I'm trying to learn C# from some YouTube videos, but I have a few
I am new to PHP and trying to learn some so sorry if this
I'm trying to learn some WCF on my own. I have C#/ASP.net knowledge but
I am trying to learn a java-based program, but I am pretty new to
I'm completely new to the javascript and ajax world but trying to learn. Right
I am trying to learn OO and classes and all that good stuff in
I'm new to PHP and everything, so I'm trying to learn some things. I'm
I'm trying to learn C++ using Qt for some basic visual applications. I want

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.