Because you say "when another button is clicked", I assume you are trying to make it so there are multiple buttons, each one loading a different image.
If this is indeed the case, I'd go with the following approach:
// Array of image objects to keep things neat
var images = [
{ "src": "http://placehold.it/120x160", "width": "120", "height": "160", "alt": "Test Image 1" },
{ "src": "http://placehold.it/120x161", "width": "120", "height": "160", "alt": "Test Image 2" }
];
function show_image(image) {
// Ensure provided index is in array
if (image < images.length) {
// Remove this image if already displayed
if (document.getElementsByClassName("imageNo" + image)[0] !== undefined)
document.getElementsByClassName("imageNo" + image)[0].outerHTML = "";
else {
// Remove other images if displayed
if (document.getElementById("currentImage") !== null)
document.getElementById("currentImage").outerHTML = "";
var img = document.createElement("img");
var cur = images[image];
img.id = "currentImage";
img.className = "imageNo" + image;
img.src = cur.src;
img.width = cur.width;
img.height = cur.height;
img.alt = cur.alt;
document.body.appendChild(img);
}
}
else
console.log('No image exists for this index');
}
// Add functionality to buttons
document.getElementById("imageButton1").onclick = function() {show_image(0);}
document.getElementById("imageButton2").onclick = function() {show_image(1);}
Fiddle
If you have one button that you'd like to cycle through all the images, you could use the same approach but change show_image to
var currentImage = 0;
function show_image() {
// Remove other images if displayed
if (document.getElementById("currentImage") !== null)
document.getElementById("currentImage").outerHTML = "";
var img = document.createElement("img");
var cur = images[currentImage++ % images.length];
img.id = "currentImage";
img.src = cur.src;
img.width = cur.width;
img.height = cur.height;
img.alt = cur.alt;
document.body.appendChild(img);
}
function hide_image() {
// Remove other images if displayed
if (document.getElementById("currentImage") !== null)
document.getElementById("currentImage").outerHTML = "";
currentImage = 0;
}
// Add functionality to buttons
document.getElementById("imageButton1").onclick = function() {show_image();}
document.getElementById("imageButton2").onclick = function() {hide_image();}
Fiddle
Hope one of these is what you are looking for.
EDIT:
For a multidimensional array you'll need to do the following:
// Array of image objects to keep things neat
var images = [
//group 1
[
{ "src": "http://placehold.it/120x160", "width": "120", "height": "160", "alt": "Test Image 1" },
{ "src": "http://placehold.it/120x161", "width": "120", "height": "160", "alt": "Test Image 2" }
],
//group 2
[
{ "src": "http://placehold.it/120x160", "width": "120", "height": "160", "alt": "Test Image 3" },
{ "src": "http://placehold.it/120x161", "width": "120", "height": "160", "alt": "Test Image 4" }
],
];
Obviously if you are going to have 800 images, this is going to be a bit more tedious. Are all the images going to be the same width/height? If they are, I'd take the hit on SEO to go for something like this:
var imageUrls = [
"http://placehold.it/120x160",
"http://placehold.it/120x160",
//etc.
];
// Change this as desired
var itemsPerGroup = 40;
// Returns how many times imageUrls length can be divided into 40 for group count
var images = new Array(Math.floor(imageUrls.length/itemsPerGroup) + 1);
// Initializes child arrays, ensuring last is only as long as it needs to be
for (var i=0; i<images.length; i++)
images[i] = (i !== images.length - 1) ? new Array(itemsPerGroup) : new Array(images.length % itemsPerGroup);
// Fill arrays
for (var i=0; i<imageUrls.length; i++) {
// current group
var group = Math.floor(i/itemsPerGroup);
// dividend is item number
var item = i%itemsPerGroup;
images[group][item] = {
"src": imageUrls[i],
"alt": "Group " + group + ", Image " + item,
"width": "120",
"height": "160"
};
}
Using this you'll call images like so
// Array of currentImages
var currentImages = new Array(images.length);
// Initialize
for (var i=0; i<currentImages.length; i++)
currentImages[i] = 0;
function show_image(group) {
var imageID = "currentImage" + group;
var imageContainer = "imageGroup" + group + "Container";
// Remove other image in group if displayed
if (document.getElementById(imageID) !== null)
document.getElementById(imageID).outerHTML = "";
var img = document.createElement("img");
var cur = images[group][currentImages[group]++%images[group].length];
img.id = "currentImage" + group;
img.src = cur.src;
img.width = cur.width;
img.height = cur.height;
img.alt = cur.alt;
document.getElementById(imageContainer).appendChild(img);
}
function hide_image(group) {
// Remove image in group if displayed
var imageID = "currentImage" + group;
if (document.getElementById(imageID) !== null)
document.getElementById(imageID).outerHTML = "";
// Reset currentImage
currentImages[group] = 0;
}
And of course, a Fiddle showing it in action.