1

Page I reference.

Part where images are loaded:

<div id="lighttable">

<img src="http://throwingupthew.com/girls/01.jpg" alt="01" />
<img src="http://throwingupthew.com/girls/02.jpg" alt="02" />
<img src="http://throwingupthew.com/girls/03.jpg" alt="03" />

etc....

</div>

How do i randomize the order of the images? Please keep in mind i will not know where to place any code/answer in index.html

Thank you.

L84
  • 45,514
  • 58
  • 177
  • 257

1 Answers1

0

Change your HTML to this :

<div id="lighttable">

<img id="girl1" src="" alt="01" />
<img id="girl2" src="" alt="02" />
<img id="girl3" src="" alt="03" />

etc....

</div>

Put this right before your page's </head> tag

<script type="text/javascript">
var array = ['http://throwingupthew.com/girls/01.jpg','http://throwingupthew.com/girls/02.jpg','http://throwingupthew.com/girls/03.jpg'];
function shuffle(array) {
var currentIndex = array.length;
var temporaryValue;
var randomIndex;
  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
  return array;
}
var shuffled_images = shuffle(array);
for(var i=0;i<3;i++) {
document.getElementById('girl' + (i+1)).src = shuffled_images[i] ;
}
</script>

Thanks to this page Randomizing(Shuffling) an Array, that I was able to make this script up for you :)

Here's the working JSFiddle :)

Community
  • 1
  • 1
prateekkathal
  • 3,482
  • 1
  • 20
  • 40