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

The Archive Base Latest Questions

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

I’m trying to loop thru an array of items and put each of them

  • 0

I’m trying to loop thru an array of items and put each of them inside a couple of columns. I’ve managed to do this, but I would like order to be slightly different.

I’ve tried to illustrate what I’m trying to achieve and what my problem is here: http://jsfiddle.net/yXGA7/9/

If you click the “Prepend” link, six divs will appear in the first column box. But, the order is not what I’m looking it to be. I would like it to be read (from top left):

5 4 3 2
1 0

and not:

1 0 3 2
5 4

I’m aware of that something needs to done here:

colCounter = 1;
cols = 4;

$("#prepend").click(function(){
  $.each(makeDivs(), function (index, value) {
    var item = $(value);
    $("#col" + colCounter).prepend(item);
    colCounter++;
    if(colCounter > cols) {
      colCounter = 1;
    }
  });  
})

But not sure what to do.

Anyone owho would like to help me out with this?

UPDATE
Unfortunately, I need to use the Prepend function. I believe “Append” would work, but cannot use that, I’m afraid.

UPDATE 2
I’ve updated the jsFiddle to illustrate what I would like to happen: http://jsfiddle.net/yXGA7/9/

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

    Do not reverse your makeDivs array and start at colCounter at 2 to working your ways backwards:

    $(document).ready(function() {
        var ds = makeDivs();
        var colCount = 4; 
        var colCounter = ds.length % colCount;  // simply assign to 2 if you don't want to make it dynamic
        // push out the items to the columns
    
        $("#prepend").click(function(){
          $.each(ds , function (index, value) {
            var item = $(value);
            $("#col" + colCounter).prepend(item);
            colCounter--;  // go backwards
            if(colCounter < 1) {  
              colCounter = colCount; //reset to 4
            }
          });  
        })
    });
    function makeDivs() {
       var divs = new Array();   
       for(var i=0;i<6;i++){
          div = $('<div></div>').addClass('item');
          p = "<p>"+i+"</p>";
          div.append(p);
          divs.push(div);
        }
    
      return divs; // don't reverse
    }
    

    Be careful with global variable declarations with i, colCounter, etc.

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

Sidebar

Related Questions

Trying to loop thru a result that has items. Items have a type and
I'm trying to loop some array using foreach . This is the code which
am trying to loop and get the values of names from this array ,
I'm stuck trying to loop through this array of GPS coordinates that puts pins
I'm trying to loop through an object like you would an array. I'm struggling
I'm trying to loop through a number of items, and create a json object.
I am trying to loop though my users database to show each username in
I am trying to loop from 100 to 0. How do I do this
I am trying to loop through a directory of text files and combine them
I'm trying to loop through the results from the Last.fm API but it's not

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.