mcmuney Posted July 27, 2006 Share Posted July 27, 2006 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] Quote Link to comment Share on other sites More sharing options...
nogray Posted July 28, 2006 Share Posted July 28, 2006 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] Quote Link to comment Share on other sites More sharing options...
mcmuney Posted July 31, 2006 Author Share Posted July 31, 2006 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] Quote Link to comment Share on other sites More sharing options...
nogray Posted July 31, 2006 Share Posted July 31, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.