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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:13:57+00:00 2026-06-17T22:13:57+00:00

I am basically trying to dynamically add an Array of images randomly into their

  • 0

I am basically trying to dynamically add an Array of images randomly into their own li elements, which is working fine, but what I really want to do now is append a second image per list element. I’m not sure how this would work. Here’s my working randomiser script which shuffles the array order:

var imgs = ['img-1.jpg','img-2.jpg','img-3.jpg','img-4.jpg','img-5.jpg','img-6.jpg'];

var i = imgs.length, j, tempi, tempj;
while (i--) {
    j = Math.floor(Math.random() * (i + 1));
    tempi = imgs[i];
    tempj = imgs[j];
    imgs[i] = tempj;
    imgs[j] = tempi;

    var img = imgs[i];
    var folder = 'img/';

    var li = document.createElement('li');
    li.className = 'phototickr';
    document.getElementById('stream').appendChild(li);

    var tickr = document.createElement('img');
    tickr.src = folder + img;
    tickr.alt = '';

    li.appendChild(tickr);
}

The final markup I want is this:

<ul id="stream">
<li><img src="img-1.jpg"><img src="img-2.jpg"></li>
<li><img src="img-3.jpg"><img src="img-4.jpg"></li>
<li><img src="img-5.jpg"><img src="img-6.jpg"></li>
</ul>

Not sure how to get my head around it, or if there is a clean solution, much appreciated any help. And no, I don’t want to use jQuery.

  • 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-17T22:13:58+00:00Added an answer on June 17, 2026 at 10:13 pm

    Shuffle the array in advance and then the task becomes much clearer, but you need to make an allowance for if you have an odd number of images.

    var imgs = ['img-1.jpg','img-2.jpg','img-3.jpg','img-4.jpg','img-5.jpg','img-6.jpg'];
    
    function shuffleArray(a) { // Fisher-Yates shuffle
        var i = a.length, t, j;
        while (--i) t = a[i], a[i] = a[j = ~~(Math.random() * (i+1))], a[j] = t;
    }
    shuffleArray(imgs);
    
    var i = imgs.length, stream, li, img1, img2;
    stream = document.getElementById('stream');
    while ((i = i - 2) >= 0) {
        // <li>
        li = document.createElement('li');
        li.className = 'phototickr';
        // <img>
        img1 = document.createElement('img');
        img1.src = 'img/' + imgs[i];
        img1.alt = '';
        img2 = document.createElement('img');
        img2.src = 'img/' + imgs[i + 1];
        img2.alt = '';
        // append
        li.appendChild(img1);
        li.appendChild(img2);
        stream.appendChild(li);
    }
    if (i === -1) { // odd number of images
        // <li>
        li = document.createElement('li');
        li.className = 'phototickr';
        // <img>
        img1 = document.createElement('img');
        img1.src = 'img/' + imgs[0];
        img1.alt = '';
        // append
        li.appendChild(img1);
        stream.appendChild(li);
    }
    

    Example fiddle (with broken images)

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

Sidebar

Related Questions

I'm trying to build a multidimensional array dynamically. What I want is basically this
So I'm trying to to basically dynamically create li's inside an array, and I
ok so I am trying to dynamically create a select list using javascript, basically
I'm basically a C++ guy trying to venture into C#. From the basic tutorial
I'm trying to generate a form dynamically, based on a template which in turn
I want to dynamically create an image for a UITableViewCell which is basically a
Hello I'm trying to dynamically generate some inputs for my form, but it's not
I'm trying to dynamically build a javascript array where _tags is a globally defined
So I'm trying to dynamically load a usercontrol into a placeholder in another usercontrol
I am trying to dynamically allocate an array of base(Student) classes, and then assign

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.