So this is a pretty straightforward problem i am encountering. I am trying to set up a web page for one of my school assignments. It is an online order form with images of what we are selling. I'm selling t-shirts and I start by displaying a thumbnail of the design on the shirt. I would like to have the capacity to have people click on the thumbnail, and have the image swap to the full image of the shirt. I have put both of the files in the same directory as the index.html as well as java and php files. most of my HTML is generated using javascript and i just cant figure out why it wont work. Showcase is the name of my div i wanted to display the pictures in.
The code to generate the img div holding my thumbnail:
Showcase.innerHTML += '< img src="GorgeThumb.jpg" alt="image not found" id="MiniGorge" onclick = "ImgSwap()">'
And the thumbnail shows up fine. However when i try to switch them it doesnt work. This is my function::
function ImgSwap()
var MetalMini = (document.getElementById("MiniGorge"))
if (MetalMini.src === "GorgeThumb.jpg")
{
document.getElementById("MiniGorge").src = "Metallica-Gorge.jpg";
}
else if (MetalMini.src === "Metallica-Gorge.jpg")
{
document.getElementById("MiniGorge").src = "GorgeThumb.jpg";
}
Im sure someone out there will scoff at how simple this solution is but if anyone can help with this im happy to take the shaming! :D.
I tried the Answers below and couldn't get them to work. Just an update: If i use this:
enter code here
function ImgSwap()
{
var MetalMini = document.getElementById("MiniGorge");
if (MetalMini.src = "GorgeThumb.jpg")
{
MetalMini.src = "Metallica-Gorge.jpg";
}
}
It seems to work fine. It switches to the large image. However if i use
function ImgSwap()
{
var MetalMini = document.getElementById("MiniGorge");
if (MetalMini.src = "GorgeThumb.jpg")
{
MetalMini.src = "Metallica-Gorge.jpg";
}
if (MetalMini.src = "Metallica-Gorge.jpg")
{
MetalMini.src = "GorgeThumb.jpg";
}
}
all of it fails and there are no image swaps at all.
Figrued it out!
So i just made way too much code but figured it out. I just call 2 separate methods onmouseover and onmouseout. Thank you all for your help though!
Solution:
Showcase.innerHTML += '<img src="GorgeThumb.jpg" id="MiniGorge" onmouseover="ImgSwapGorge()" onmouseout="ImgRevGorge()">';
function ImgSwapGorge()
{
var MetalMini = document.getElementById("MiniGorge");
if (MetalMini.src = "GorgeThumb.jpg")
{
MetalMini.src = "Metallica-Gorge.jpg";
MetalMini.id = "MegaGorge";
}
else
{
MetalMini.src = "GorgeThumb.jpg";
}
}
function ImgRevGorge()
{
var MetalMini = document.getElementById("MegaGorge");
MetalMini.src = "GorgeThumb.jpg";
MetalMini.id = "MiniGorge";
}
So hopefully if someone out there is looking for a (inefficient) but working code to switch images here it is!