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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:03:51+00:00 2026-05-23T05:03:51+00:00

If you fiddle with tag string, for example by putting space between >< ,

  • 0

If you fiddle with tag string, for example by putting space between ><, the effect is different.

<!DOCTYPE html>
<head><title>insertBefore error</title></head>
<body>
<div id='myElement'>above this div 3 elements should be inserted
but actually only 2 are, text2 is missing</div>
<script  type="text/javascript">
    referenceTag = document.getElementById('myElement');
    var newElement = document.createElement('div');
    newElement.innerHTML = '<div>text1</div><pre>text2</pre><p>text3</p>';
    for (i=0; i<newElement.childNodes.length; i++) {
        alert(newElement.childNodes[i].tagName);
        //if you comment-out following line there are 3 alerts but otherwise only two.
        referenceTag.parentNode.insertBefore(newElement.childNodes[i],referenceTag);
    }
</script>
</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-05-23T05:03:52+00:00Added an answer on May 23, 2026 at 5:03 am

    When you call insertBefore you move the node so it becomes a child of referenceTag.parentNode. At this point it is no longer a child of newElement so newElement.childNodes shrinks by one.

    You then increment i, so you keep skipping elements.

    i.e.

    1. You move node 0
    2. What was node 1 becomes node 0
    3. You move the new node 1 and leave the old node 1 (and current node 0) behind.

    You probably want:

    while (newElement.firstChild) {
            alert(newElement.firstChild.tagName);
            referenceTag.parentNode.insertBefore(newElement.firstChild,referenceTag);
    }
    

    If you fiddle with tag string, for example by putting space between ><, the effect is different.

    The effect is actually the same, but harder to see. Inserting spaces means that instead of having a data structure along the lines of:

    [ elementNode, elementNode, elementNode ]
    

    You have:

    [ elementNode, textNode, elementNode, textNode, elementNode ]
    

    Since you skip every other node, and every other node is a text node consisting entirely of white space, you can’t see the effect.

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

Sidebar

Related Questions

I set up a fiddle with the following code: html: <div id=content> <input id=splash_button
If I create an HTML anchor tag and set the disabled attribute to true,
In the fiddle posted here: example clicking on 'Check Chapter 4' takes me to
I am just starting to fiddle with Excel via C# to be able to
I am new to Haskell and trying to fiddle with some test cases I
Like in fiddler you can watch the logs between client and server. Like fiddler;
For a pet project I started to fiddle with ANTLR. After following some tutorials
i have a very simple question I can't seem to get my head around.
I have a textarea tag that has text I want a user to edit.
Fiddle here: http://jsfiddle.net/cgweb87/r3NEX/ When you click on the h2, it slides the div up

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.