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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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