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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:16:01+00:00 2026-06-06T05:16:01+00:00

I am trying dynamically add children DIV under a DIV with ID=prnt. Addition of

  • 0

I am trying dynamically add children DIV under a DIV with ID=”prnt”. Addition of nodes work fine no problem. However strange enough when it comes to deleted nodes its only deleting the even numbered nodes including 0. Why is this, I could be something stupid but it seem more like a bug. I could be very wrong.

    <script type="text/javascript">

        function displayNodes()
        {
            var prnt = document.getElementById("prnt");
            var chlds = prnt.childNodes;
            var cont = document.getElementById("content");

            for(i = 0; i < chlds.length; i++)
            {
                if(chlds[i].nodeType == 1)
                {

                    cont.innerHTML +="<br />";
                    cont.innerHTML +="Node # " + (i+1);
                    cont.innerHTML +="<br />";
                    cont.innerHTML +=chlds[i].nodeName;
                    cont.innerHTML +="<br />";

                }
            }
        }

        function deleteENodes()
        {
            var prnt = document.getElementById("prnt");
            var chlds = prnt.childNodes;


            for(i = 0; i < chlds.length; i++)
            {

                    if(!(chlds[i].nodeType == 3))
                    {
                        prnt.removeChild(chlds[i]);
                    }

            }


        }

        function AddENodes()
        {
            var prnt = document.getElementById("prnt");

           //Only even nodes are deletable PROBLEM

            for(i = 0; i < 10; i++)
            {

                var newDIV = document.createElement('div');
                newDIV.setAttribute("id", "c"+(i));

                var text = document.createTextNode("New Inserted Child "+(i));
                newDIV.appendChild(text);
                prnt.appendChild(newDIV);

            }
        }

    </script>

    <title>Checking Div Nodes</title>
</head>


<body>

    <div id="prnt">
        Parent 1

    </div>

    <br />
    <br />
    <br />

   <button type="button" onclick="displayNodes()">Show Node Info</button>
   <button type="button" onclick="deleteENodes()">Remove All Element Nodes Under Parent 1</button>
   <button type="button" onclick="AddENodes()">Add 5 New DIV Nodes</button>

    <div id="content">

    </div>
</body>

  • 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-06T05:16:03+00:00Added an answer on June 6, 2026 at 5:16 am

    The problem is you’re modifying the collection as you loop through it. Iterating in reverse should fix this…

    for(i = chlds.length-1; i >= 0; i--)
    {
        if(!(chlds[i].nodeType == 3))
        {
             prnt.removeChild(chlds[i]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

as the title states, I am trying to add children to a dynamically added
I'm trying to dynamically add content stored in a variable. However, single quotes are
I'm trying to use dynamically JTree component. Under root node I have four nodes
I'm trying to dynamically add buttons to the userform, but the userform just comes
I am trying to dynamically add input fields, which I am able to do.
I am trying to add the button dynamically when list is empty i.e no
I am trying to add a html snippet dynamically which contains named anchors and
I am trying to add views to LinearLayout dynamically as much as it possible
we are trying to add ColumnDefinition to a Grid in WPF dynamically from code
I am trying and failing to add datepicker to inputs that are created dynamically.

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.