Log in

View Full Version : onClick to change image but with a variable passed



Stig
07-13-2007, 02:12 PM
I have the simplest js function to change an image on a page -

function swapImage() {
this.document.IMG1.src = 'propertyimages/PWI07071360/slides/image1.jpg'
return(false);
}

which works perfectly. The problem is I need the path to the image to contain a php variable. I tried this

function swapImage(thisImage) {
this.document.IMG1.src = 'propertyimages/' + thisImage + '/image2.jpg'
return(false);
}

Where thisImage is passed as the following - PWI07071360, but it's not working at all. Am I building the string incorrectly in the function?

I also tried this -

function swapImage(thisImage) {
newImageName = 'propertyimages/' + thisImage + '/slides/image2.jpg';
this.document.IMG1.src = newImageName
return(false);
}

I'd welcome any help or suggestions please.

Twey
07-13-2007, 09:52 PM
function swapImage(thisImage) {
document.images['IMG1'].src = "propertyImages/" + thisImage + "/slides/image2.jpg";
}

Stig
07-13-2007, 11:18 PM
Thanks. That works perfectly now.
(I almost passed a second parameter to specify the image number from 1 to 9 but instead I went with 9 separate javascript functions. That way the .php looks way more complex and my boss is way more impressed).

Twey
07-14-2007, 12:10 AM
Haha, oh dear. You may have earned the respect of your boss, but I think at the cost of the derision of any other web developers who encounter your code :)