Porl123 Posted February 18, 2009 Share Posted February 18, 2009 I have images on a page that all have the same name. I want to change the source to all the images at once but I'm not sure how to do it. It's sort of the PHP foreach equivilent I need. Can anyone suggest a method? Any help would be appreciated! Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 18, 2009 Share Posted February 18, 2009 Some code showing your current structure would have been helpful <html> <head> <script type="text/javascript"> function changeImages(imgName, imageSrc) { var imageList = document.images; for (var imgIdx=0; imgIdx<imageList.length; imgIdx++) { if(imageList[imgIdx].name==imgName) { imageList[imgIdx].src = imageSrc; } } return; } </script> </head> <body> <img name="test1" src="image1.jpg"> <img name="test1" src="image1.jpg"><br><br> <img name="test2" src="image2.jpg"> <img name="test2" src="image2.jpg"><br><br> <button onclick="changeImages('test1', 'image1.jpg');">Change Test 1 to Image 1</button> <button onclick="changeImages('test1', 'image2.jpg');">Change Test 1 to Image 2</button> <button onclick="changeImages('test2', 'image1.jpg');">Change Test 2 to Image 1</button> <button onclick="changeImages('test2', 'image2.jpg');">Change Test 2 to Image 2</button> </body> </html> 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.