d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 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:41pm
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: 22,030
Joined: Apr 28 2006
Gold: 1,283.00
Jul 26 2014 08:30pm
Figured it out, this can be closed.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Jul 27 2014 05:49am
Didn't post your solution? :(
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll