/    Sign up×
Community /Pin to ProfileBookmark

how to reverts 2 images back to the original images using onClick event handler

Beginning images:

Example: A B

When you click on img A // this change B to C
img B // this change A to D

now we have: D C
I am trying to figure out how I can reverts these images back to A B as in the beginning.

I have try using another function such as

function changeImgBack

but I can’t get it to change…I also tried multiple ways but not luck. I would appreciate your help on this matter. Here is what I have so far…I didn’t included the different ways I have tried.

if (document.images) {
// if images are supported by the browser, create an array of images and preload them

var myImage = new Array() // list Images

myImage[0] = new Image()
myImage[0].src = “images/image1.gif”
myImage[1] = new Image()
myImage[1].src = “images/image2.gif”
myImage[2] = new Image()
myImage[2].src = “images/image3.gif”
myImage[3] = new Image()
myImage[3].src = “images/image4.gif”

function changeImg(valueClick) {
if (valueClick == 0) {
document.images[1].src = myImage[2].src
} else if (valueClick == 1) {
document.images[0].src = myImage[3].src
} else {
alert(“Houston, we have a problem”)
}

return false; //prevent click from going anywhere
}

}

</script>
</head>
<body>
<table width=”300″ border=”0″>
<tr>
<td><a href=”Lab4.2.html” onClick=”changeImg(0);return false;”><img src=”images/image1.gif” name=”img1″ width=”300″ height=”150″

>

</a></td>
<td><a href=”Lab4.2.html” onClick=”changeImg(1);return false;”><img src=”images/image2.gif” name=”img2″ width=”300″ height=”150″

>

</a></td>
</tr>
</table>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzSep 25.2005 — You need to save the original value of each image's src attribute in an array.
Copy linkTweet thisAlerts:
@pj_59authorSep 25.2005 — Thanks for your help. ?
×

Success!

Help @pj_59 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...