Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript JS to reveal images in same place

Afternoon all,

What I need is instead of clicking the image to move to the next image I want to be able to chose to click on text "Image 1" or "Image 2" or "Image 3" to be able to be able reveal those images.

On some pages there maybe the need for just one image or up to five images.
Sometimes they may be .gig or .png files. That should be ok to to change the file types in the Var Images section ... correct ? ?

Thanks in advance,

HTML:
<!DOCTYPE html>
<html>
<head>
    <script>
        var images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
        for (var j = images.length; j--;) {
            var img = new Image();
            img.src = images[j];
        }
    </script>
</head>
<body>
This is the test page<br />
    <img id="myButton" src="image2.jpg" width="50%"/>
<br />
Image 1   <br />
Image 2   <br />
Image 3   <br /><br />
What I need is instead of clicking the image to move to the next image I want to be able to chose to click on text "Image 1" or "Image 2" or "Image 3" to be able to be able reveal those images.
<br />
On some pages there maybe the need for just one image or upto five images.<br />
Sometimes they may be .gig or .png files. That should be ok to to change the file types in the Var Images section ... correct ? ?


    <script type="text/javascript">
        var i = 1; // Initialize the index for the current image

        // Add an event listener to the image
        document.getElementById('myButton').addEventListener('click', function() {
            // Toggle to the next image in the array
            this.src = images[i >= images.length - 1 ? i = 0 : ++i];
        }, false);
    </script>
</body>
</html>
 
Last edited by a moderator:
Hey there.

Something like this should do it:
JavaScript:
const images = ["image1.jpg", "image2.jpg", "image3.jpg"];
const image = document.querySelector("#image");  // or document.getElementById("image")

function switchImage(index) {
    image.src = images[index];
}
HTML:
<img src="image1.jpg" id="image" />

<button onclick="switchImage(0)">Image 1</button>
<button onclick="switchImage(1)">Image 2</button>
<button onclick="switchImage(2)">Image 3</button>
 
Hey there.

Something like this should do it:
JavaScript:
const images = ["image1.jpg", "image2.jpg", "image3.jpg"];
const image = document.querySelector("#image");  // or document.getElementById("image")

function switchImage(index) {
    image.src = images[index];
}
HTML:
<img src="image1.jpg" id="image" />

<button onclick="switchImage(0)">Image 1</button>
<button onclick="switchImage(1)">Image 2</button>
<button onclick="switchImage(2)">Image 3</button>
I've thried this but it doesn't work sadly.

I also tried the following code from a youtube and that also doesn't work.

Code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    
<img id="bannerImage" src="image1.jpg" width="600" />
<br />
<br />
<button onClick="changeimage('image1.jpg')">Image 1</button>
<button onClick="changeimage('image2.jpg')">Image 2</button>
<button onClick="changeimage('image3.jpg')">Image 3</button>
<button onClick="changeimage('image4.jpg')">Image 4</button>
    
    
<script>
function ChangeImage(fileName){
        let img = document.querySelector('#bannerImage');
    img.setAttribute('src',fileName);
    
}
    
</script>
</body>
</html>

Anyone with any idea?
I am using Dreamweaver as the editor and I have Xampp installed to test my code
 
To add:

Can this be used instead of buttons
<a href="#" onClick="changeimage('image1.jpg')">Image One</a>
<a href="#" onClick="changeimage('image2.jpg')">Image Two</a>
<a href="#" onClick="changeimage('image3.jpg')">Image Three</a>
 
Can this be used instead of buttons
<a href="#" onClick="changeimage('image1.jpg')">Image One</a>
<a href="#" onClick="changeimage('image2.jpg')">Image Two</a>
<a href="#" onClick="changeimage('image3.jpg')">Image Three</a>
That should work.

I've thried this but it doesn't work sadly.

I also tried the following code from a youtube and that also doesn't work.

Code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
   
<img id="bannerImage" src="image1.jpg" width="600" />
<br />
<br />
<button onClick="changeimage('image1.jpg')">Image 1</button>
<button onClick="changeimage('image2.jpg')">Image 2</button>
<button onClick="changeimage('image3.jpg')">Image 3</button>
<button onClick="changeimage('image4.jpg')">Image 4</button>
   
   
<script>
function ChangeImage(fileName){
        let img = document.querySelector('#bannerImage');
    img.setAttribute('src',fileName);
   
}
   
</script>
</body>
</html>

Anyone with any idea?
I am using Dreamweaver as the editor and I have Xampp installed to test my code
Function names in JavaScript are case-sensitive, in the js you've defined it as ChangeImage, but in the html it's changeimage.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom