Jump to content

Image Swap


mcmuney

Recommended Posts

Hi, I'm using the javascript below for image swapping. But what I want to do is apply the same image swapping to several image, all identical. Basically the identical function, displayed several times. For example, I'm using a plus sign, when clicked, it becomes the minus sign.

QUESTION: When I apply the code to multiple images, it ONLY swaps the first image and when the other images are clicked, it still ONLY swaps the first image and not the respective images. How can I achieve this without duplicating case 1 and case 2 function???

[code]
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JavaScript>
intImage = 2;
function swapImage() {
switch (intImage) {
case 1:
  IMG1.src = "http://www.microsoft.com/library/toolbar/images/mslogo.gif"
  intImage = 2
  return(false);
case 2:
  IMG1.src = "http://msdn.microsoft.com/msdn-online/start/images/msdn-logo.gif"
  intImage = 1
  return(false);
}
}
</SCRIPT>
</HEAD>
<BODY>
<IMG id="IMG1" name="IMG1" src="http://www.microsoft.com/library/toolbar/images/mslogo.gif"
onclick="swapImage();">
<IMG id="IMG1" name="IMG1" src="http://www.microsoft.com/library/toolbar/images/mslogo.gif"
onclick="swapImage();">
<IMG id="IMG1" name="IMG1" src="http://www.microsoft.com/library/toolbar/images/mslogo.gif"
onclick="swapImage();">
</BODY>
</HTML>
[/code]
Link to comment
Share on other sites

you'll need to change the image ids and names so each is uniqe (IMG1, IMG2, IMG3, etc..) then pass it to the function

[code]
<SCRIPT LANGUAGE=JavaScript>
intImage = 2;
function swapImage(img) {
switch (intImage) {
case 1:
  img.src = "http://www.microsoft.com/library/toolbar/images/mslogo.gif"
  intImage = 2
  return(false);
case 2:
  img.src = "http://msdn.microsoft.com/msdn-online/start/images/msdn-logo.gif"
  intImage = 1
  return(false);
}
}
</SCRIPT>
[/code]
and your onclick event will look like
[code]
<IMG id="IMG1" name="IMG1" src="http://www.microsoft.com/library/toolbar/images/mslogo.gif"
onclick="swapImage(this);">
[/code]
Link to comment
Share on other sites

I've followed your instructions, but the images are not switching on click, see my code below. What am I missing?:

[code]
<SCRIPT LANGUAGE=JavaScript>
intImage = 2;
function swapImage(IMG) {
switch (intImage) {
case 1:
  IMG.src = "../images/plus.gif"
  intImage = 2
  return(false);
case 2:
  IMG.src = "../images/minus.gif"
  intImage = 1
  return(false);
}
}
</SCRIPT>
[/code]

Code on image:

[code]
<a href="javascript:toggle('g1');" class="faq_a"><img src="../images/plus.gif" align=left border=0 id="IMG1" name="IMG1" onclick="swapImage(this);">What Is ROM?<br></a>
<a href="javascript:toggle('g2');" class="faq_a"><img src="../images/plus.gif" align=left border=0 id="IMG2" name="IMG2" onclick="swapImage(this);">Is ROM FREE?<br></a>
[/code]
Link to comment
Share on other sites

The image will swap when you click on it, if you want the image to change when the link is clicked, you gotta add the onclick even to the link instead of the image.
[code]
<a href="#" class="faq_a" onclick="return swapImage(document.getElementById('IMG1'));"><img src="../images/plus.gif" align=left border=0 id="IMG1" name="IMG1">What Is ROM?<br></a>
<a href="#" class="faq_a" onclick="return swapImage(document.getElementById('IMG2'));"><img src="../images/plus.gif" align=left border=0 id="IMG2" name="IMG2">Is ROM FREE?<br></a>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.