Solution 1 :

On your img tags the onclick function is not wrote correctly should look like

onClick="changeImage()"

Also “changeImage” is not a function you wrote a function called “changePicture” so it should really look like

onClick="changePicture()"

Now in your changePicture function there are a few issues specifically the getElementById function is not being used correctly also you got the name wrong on it you capitalized the ‘D’ at the end which shouldn’t be, I would recommend changing the id on your header tag img from ‘Lilly’ to ‘preview’ since element id’s need to be unique and you’ve already used ‘Lilly’ as an id than replace your script tag with this

<script>
    document.getElementById("Lilly").addEventListener("click", function(){
        document.getElementById("preview").src = "Skelton_ImageSwap/image/Lilly.jpg"
    });
    document.getElementById("Lotus").addEventListener("click", function(){
        document.getElementById("preview").src = "Skelton_ImageSwap/image/Lotus.jpg"
    });
    document.getElementById("Roses").addEventListener("click", function(){
        document.getElementById("preview").src = "Skelton_ImageSwap/image/Roses.jpg"
    });
    document.getElementById("Tulip").addEventListener("click", function(){
        document.getElementById("preview").src = "Skelton_ImageSwap/image/Tulip.jpg"
    });
</script>

Problem :

I am trying to create an Image Swap with Javascript. Can anyone point me in the direction of where my coding error is in JavaScript? Am I using the getElementByID incorrectly? Any help or direction is much appreciated!

<title>Photo Gallery</title>
</head>
    <header>
        <h1>Gallery</h1>
        <div>
            <img alt="images" src="Skelton_ImageSwap/images/Lilly.jpg" 
            style="height: 350px; width: 350px" id="Lilly">
        </div>
    </header>
    <body>
    <img src="Skelton_ImageSwap/images/Lilly.jpg" alt ="images" id ='Lilly' onclick.changeImage()>
    <img src ="Skelton_ImageSwap/images/Lotus.jpg" alt = "images" id='Lotus' onclick.changeImage()>
    <img src="Skelton_ImageSwap/images/Roses.jpg" alt="images" id='Roses'onclick.changeImage()>
    <img src="Skelton_ImageSwap/images/Tulip.jpg" alt="images" id='Tulip'onclick.changeImage()>
    </body>

            <script>
                function changePicture()
                {
                     if (image.getElementByID.onclick.changePicture == "Lilly")
                {
                    image.src = "Skelton_ImageSwap/image/Lilly.jpg";
                }
                if (image.getElementByID.onclick.changePicture == "Orchid")
                {
                    image.src = "Skelton_ImageSwap/image/Orchid.jpg";
                }
                if (image.getElementByID.onclick.image == "Roses")
                {
                    image.src = "Skelton_ImageSwap/image/Roses.jpg";
                }
                else
                {
                     image.src = "Skelton_ImageSwap/image/Tulip.jpg";
                }
            }
            </script>

Comments

Comment posted by LcSkelt

With the image swap I am trying to have it where when the thumbnail picture is clicked on it will take place of

image. Sorry for not clarifying.

Comment posted by the documentation for

There’s a whole range of issues with your code, but I think

Comment posted by Siguza

I also suggest looking up how to open the web developer console for your browser, as that should aid you greatly in debugging.

Comment posted by bleepbloopblop123

Also make sure the src you’re setting them as in the script tag is the correct src for your project I see you using “Skelton_ImageSwap/images/” in your html but in the script you’re setting them as “Skelton_ImageSwap/image/” do you have both an image and images directory in your project

Comment posted by LcSkelt

Thank you for your help! I can definitely see where I had taken a wrong turn now. I think I was trying to make it much more complicated than it needed to be.

Comment posted by bleepbloopblop123

No problem if you need a different solution let me know

By