...

View Full Version : Button to switch



Lincoln
05-26-2004, 12:51 PM
Hi,
I have to images A and B
Onclick of a button, A and B would change places,
OnClick of the button again, they would return to their original position.
How do I do that?

Lincoln

glenngv
05-26-2004, 01:09 PM
function switchImage(img1, img2){
var objImg1 = document.images[img1];
var objImg2 = document.images[img2];
var temp = objImg1.src;
objImg1.src = objImg2.src;
objImg2.src = temp;
}
...
<img name="imgA" src="a.gif" />
<img name="imgB" src="b.gif" />
<input type="button" value="Switch" onclick="switchImage('imgA','imgB')" />

Lincoln
05-26-2004, 01:18 PM
I am sorry but can you explain this code?

glenngv
05-26-2004, 01:40 PM
switchImage function accepts 2 parameters, the name of the 2 images to swap (imgA and imgB in this case)


function switchImage(img1, img2){
var objImg1 = document.images[img1]; //get handle of first image
var objImg2 = document.images[img2]; //get handle of second image
var temp = objImg1.src; //store src of first image to a temporary variable
objImg1.src = objImg2.src; //swap image 2 to image 1
objImg2.src = temp; //swap temp (which is previously image 1) to image 2
}

Garadon
05-26-2004, 01:41 PM
document.images is a collection of all images in the document, the index can take both names of elements and number indexing, so he takes the 2 names og the img element and creates 2 variables 2 each element, then he create a 3rd variable to hold the temporary source of one of the element then he sets this elements source to the other elements source, then he sets the other elements source to the temporary holds source.


A bit of a simple image: if you hold a football in each hand and wanna swap them you need to put one of the balls down so you get 1 hand free to take the ball of the other then the free hand can pick up the ball you put down.

glenngv
05-26-2004, 01:52 PM
A bit of a simple image: if you hold a football in each hand and wanna swap them you need to put one of the balls down so you get 1 hand free to take the ball of the other then the free hand can pick up the ball you put down.
You don't have to be a juggler to easily swap the balls without putting one ball down. :D :D :D

j/k

Garadon
05-26-2004, 02:17 PM
think you meant only not don't :D

but I never seen a jugler jugling footballs.

glenngv
05-26-2004, 02:23 PM
think you meant only not don't :D

but I never seen a jugler jugling footballs.
I really meant "don't" as anybody can do it himself (maybe with several tries). :D

Garadon
05-26-2004, 02:29 PM
damnit :p perhaps we should make a coding forum competion in jugling three or more footballs.

Short CFCJTMF



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum