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 Link to comment https://forums.phpfreaks.com/topic/145822-selecting-objects-with-the-same-name/ 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> Link to comment https://forums.phpfreaks.com/topic/145822-selecting-objects-with-the-same-name/#findComment-765682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.