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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:25:38+00:00 2026-05-31T19:25:38+00:00

JSFiddle URL: http://jsfiddle.net/sFe45/ Code: <html> <head> <title>Foo</title> <script type=text/javascript> var target; var anchor; function

  • 0

JSFiddle URL: http://jsfiddle.net/sFe45/

Code:

<html>
    <head>
        <title>Foo</title>
        <script type="text/javascript">

var target;
var anchor;

function removeTags(node){
    for (var i = 0; i < node.childNodes.length; i++) {
        var childNode = node.childNodes[i];
        if (childNode.nodeType == 1) {
            node.removeChild(childNode);
            continue;
        } 
        removeTags(childNode);
    }
}

window.onload = function() {
    target = document.getElementById('target');
    anchor = document.getElementById('anchor');
    anchor.onclick = function() {
        removeTags(target);
    }
}

        </script>
    </head>
    <body>
        <div id="target">
            <p>
                <a href="#" id="anchor">Remove tags</a>
            </p>

            Text

            <p>Para 1</p>
            <p>Para 2</p>
            <p>Para 3</p>
            <p>Para 4</p>
            <p>Para 5</p>
            <p>Para 6</p>
            <p>Para 7</p>
            <p>Para 8</p>
            <p>Para 9</p>
            <p>Para 10</p>
            Text
        </div>
</html>

​As you will notice in the JSFiddle URL, when you click the “Remove tags” link, removeTags() fails to remove alternate paragraph elements. The reason is when a removeTags removes a child node from its parent, the remaining child nodes shift one place to left in node.childNodes array.

How can this be fixed in a neat way?

  • 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-31T19:25:39+00:00Added an answer on May 31, 2026 at 7:25 pm

    Count backward:

    for (var i = node.childNodes.length - 1; i >= 0; i--) {
    

    Updated fiddle

    Alternately, you can use lastChild and previousSibling:

    var prev;
    for (var child = node.lastChild; child; child = prev) {
        prev = child.previousSibling;
    
        if (child.nodeType == 1) {
            node.removeChild(child);
            continue;
        } 
    
        removeTags(child);
    }
    

    Updated fiddle

    Separately:

    …the remaining child nodes shift one place to left in node.childNodes array…

    NodeLists are not arrays.

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

Sidebar

Related Questions

http://jsfiddle.net/YcK5X/ I'm wondering why this AJAX request doesn't return anything. $.ajax({ type: 'POST', url:
Even though the following works: http://jsfiddle.net/N7D5r/ My attempt at using the same code does
Please refer the url http://jsfiddle.net/fnvXT/ Here while selecting the check box the corresponding row
I've setup a demo of my problem at the following url: http://jsfiddle.net/YHHg7/4/ I'm trying
tl;dr JSFiddle URL: http://jsfiddle.net/66Mck/ I have a Drag UI I'm working on, and I'm
the code is here http://jsfiddle.net/assuredlonewolf/vHC9A/20/ I have no clue what is going on! I
I have the following inline SVG code in my HTML5 file ( http://jsfiddle.net/Jbfw2/ ):
http://jsfiddle.net/zTTxu/ How can I animate once when page1 load example: animate 'a' to 'b'
http://jsfiddle.net/FS4zT/ If you visit the above link you see what am describing below. Summary:
http://jsfiddle.net/FYtJH/1 The filtering select needs to warn users that they're about to change the

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.