Jump to content

Selecting objects with the same name


Porl123

Recommended Posts

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

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>

Archived

This topic is now archived and is closed to further replies.

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