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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:23:57+00:00 2026-05-20T08:23:57+00:00

so i am in a bit of a jam here and having a really

  • 0

so i am in a bit of a jam here and having a really hard time trying to solve this dilemma.

Here is what i am trying to do:

There is a container div (id=posts) full of other divs (name=posts).
I want it to grab in those divs and, by even/odd position, append them inside 2 other divs.

These 2 other divs are generated by javascript, one floats left and the other floats right.

Then i want it to empty the container div and insert the floating divs.

So far i have managed everything but one thing; it is only using the first found div…

Here is the HTML code:

         <div id="posts">
            <div name="posts">
            left
            </div>
            <div name="posts">
            right
            </div>
            <div name="posts">
            left
            </div>
            <div name="posts">
            right
            </div>
            <div name="posts">
            left
            </div>
            <div name="posts">
            right
            </div>
            <div name="posts">
            left
            </div>
            <div name="posts">
            right
            </div>
        </div>

Here is the script that i have been working with so far:

    <script type="text/javascript">
        var container = document.getElementById("posts");
        var posts = document.getElementsByName("posts");
        var rdiv = document.createElement("div");
            rdiv.style.backgroundColor = "#0F0";
            rdiv.style.cssFloat = "right";
            rdiv.style.clear = "right";
        var ldiv = document.createElement("div");
            ldiv.style.backgroundColor = "#F00";
            ldiv.style.cssFloat = "left";
            ldiv.style.clear = "left";
        for (i=0; i<posts.length; i++){
            if (i%2){
                rdiv.appendChild(posts[i]);
                }else{
                ldiv.appendChild(posts[i]);
                }
            }
            container.innerHTML = "";
            container.appendChild(rdiv);
            container.appendChild(ldiv);
    </script>

Now what this is doing is that it creates the left and right floating divs normally, but it only appends the first div inside both, the rest goes kabluey, can anyone help me with this one?

  • 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-20T08:23:57+00:00Added an answer on May 20, 2026 at 8:23 am

    The main issue you are having is: appending a live element to another element, removes the element from its original location, causing problems with the loop.

    To fix this, store the elements into a temporary array, appending them to their corresponding div’s only once the looping is done:

        var temp1 = [], temp2 = [];
        for (i=0; i<posts.length; i++){
            if (i%2){
                temp1.push(posts[i]);
                }else{
                temp2.push(posts[i]);
                }
            }
        for(var i = 0; i < temp1.length; i++) {
            rdiv.appendChild(temp1[i]);
        }
        for(var i = 0; i < temp2.length; i++) {
            ldiv.appendChild(temp2[i]);
        }
    

    You can see the full working example here: http://jsfiddle.net/FqSMw/

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

Sidebar

Related Questions

i am in a bit of a jam here, i am trying to find
Bit of a JavaScript newbie here - I am firing this basic bit of
Bit of a Rails newbie here so please bear with me. I am trying
bit of a rough time getting this to work and its an understanding piece
Bit tricky to communicate but hope this makes sense. I have 8 div containers
Bit of a jquery Newbie here, so go easy. Basically I'm trying to plug
Bit of a puzzle for you.... I have a 730px wide, auto height div.
Bit of a tough one to explain here, but I'll do my best. I
Bit of a javascript newbie so not sure if this question is easy or
Bit of a weird glitch here. The tag name of a childNode is logged

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.