I think your best bet would be to add a class (let's say random
) to each img
element:
<img src="pic_10.gif" class="random" alt="" height=100 width=100 />
Then we can get all the images with a simple querySelectorAll
:
var images = document.querySelectorAll('.random');
Randomize the array of src
s:
var srcs = ['pic_01.gif', 'pic_01.gif', etc, etc];
srcs = srcs.sort(function() { return Math.random() > 0.5; });
Then iterate over the images, applying the new src to each:
for (var i = 0; i < images.length; ++i) {
images[i].src = srcs[i];
}
The full function being:
var imageSwap = function() {
var images = document.querySelectorAll('.random');
var srcs = ['pic_01.gif', 'pic_01.gif', etc, etc];
srcs = srcs.sort(function() { return Math.random() > 0.5; });
for (var i = 0; i < images.length; ++i) {
images[i].src = srcs[i];
}
};