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.
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.
Example fiddle (with broken images)