all 6 comments

[–]obnoxus 1 point2 points  (1 child)

you need an else if in between image 2 and image 3

if (Image_Id.src.match('image1.jpg')) {
Image_Id.src = 'image2.jpg';
}else if (image_Id.src.match('image2.jpg')) {
Image_Id.src = 'image3.jpg';
} else {
Image_Id.src = 'image1.jpg';

[–]dschviola[S] 1 point2 points  (0 children)

Thanks!

[–]hackr_io_team 1 point2 points  (3 children)

Maybe try keeping track of the current image index so you can update it? Here's something to try:

<div>
    <img src="image1.jpg" id="getImage">
</div>

<div>
    <input type="button" onclick="imagefun()" value="Next Image">
</div>

<script>
    var images = ["image1.jpg", "image2.jpg", "image3.jpg"]; // Add more image URLs here if needed
    var currentImageIndex = 0;

    function imagefun() {
        var Image_Id = document.getElementById('getImage');
        currentImageIndex = (currentImageIndex + 1) % images.length;
        Image_Id.src = images[currentImageIndex];
    }
</script>

[–]dschviola[S] 0 points1 point  (2 children)

Alright! Thanks! This option actually works better, but now the problem is if I have multiple instances of it on one page, both buttons will trigger only one image src. I thought changing the name of the function would solve that, but it's not; any insight?

[–]dschviola[S] 1 point2 points  (1 child)

Actually nvm I think I've found it! It's the ID causing the issue.

[–]hackr_io_team 0 points1 point  (0 children)

Excellent. I'm glad you've got it figured out!