d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Switching Images Using Javascript
Add Reply New Topic New Poll
Member
Posts: 22,030
Joined: Apr 28 2006
Gold: 1,283.00
Jul 26 2014 06:42pm
Hi everyone -

I'm currently learning JavaScript at a university level (so still pretty basic level teachings) but I'm stumped on how to perform this request.

Basically I have two images (one is a tiny blue ball and one is a tiny red ball) and a button "Change Color". They start out one Red and one Blue.

I have to design it so it's changing the color of the balls. Starting with the color combination of two balls(blue,blue), any button click must lead to the following color combinations repeated cyclically: (blue,red), (red,blue), (red,red), then again (blue,blue)...

Both balls have their own image file as well. I have the basic structure down to just switch between Red and blue over and over but I'm stumped at this point...any suggestions?

Code
<script type="text/javascript">
function lampSwitch() {
var temp = document.images["left"].src;
document.images["left"].src =
document.images["right"].src;
document.images["right"].src = temp;
}
</script>
<img src="ballred.gif" name="left" />
<img src="ballblue.gif" name="right" />
<p>
<form>
<input type="button" value="Switch"
onclick="lampSwitch()" />
</form>


Thanks!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 26 2014 07:28pm
keep an array with the sequence. each time the button is clicked, go to the next entry in the array to determine which images to show.
Member
Posts: 22,030
Joined: Apr 28 2006
Gold: 1,283.00
Jul 26 2014 08:30pm
Figured it out, thanks!
Go Back To Homework Help Topic List
Add Reply New Topic New Poll