Jump to content


Photo

Image Swap


  • Please log in to reply
3 replies to this topic

#1 mcmuney

mcmuney
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 27 July 2006 - 11:51 PM

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???

<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>


#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 28 July 2006 - 04:10 PM

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

<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>
and your onclick event will look like
<IMG id="IMG1" name="IMG1" src="http://www.microsoft.com/library/toolbar/images/mslogo.gif" 
 onclick="swapImage(this);">

NoGray.com


#3 mcmuney

mcmuney
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 31 July 2006 - 03:42 AM

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

<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 on image:

<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>


#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 31 July 2006 - 11:33 PM

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.
<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>

NoGray.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users