Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:35:27+00:00 2026-06-13T22:35:27+00:00

Ok, this is what I’m trying to do, it was too vague earlier so

  • 0

Ok, this is what I’m trying to do, it was too vague earlier so here’s a better explanation, keep in mind I’ve only been programming pages for like 2 weeks now so any help is appreciated.

I need to create a HTML/JavaScript webpage that displays three images along with a caption (description) for each image. For each image I need to also create a button that changes the first image to a different image on the same subject (and change the caption too). Also need to have a button that restores all three images back to the original images.

I got the coding to work so that one image would change to another on button click but then when I added my 2nd and 3rd coding nothing would work anymore.

<html> 
<head> 
<script> 

function changeImage()
{
var img = document.getElementById("image");
img.src="http://cdn.memegenerator.net/images/300x/159304.jpg";
return false;
}

</script>

</head>
<body>
<img id="image" src="http://thechive.files.wordpress.com/2011/10/william-defoe.jpg" />
<br><br><br>
<button id="clickme" onclick="changeImage();">Click to change image!</button>

<script> 

function changeImage()
{
var img = document.getElementById("image1");
img.src="http://playerperspective.com/wp-content/uploads/2011/05/mike-tyson-3.jpg";
return false;
}

</script>

<body>
<img id="image1" src="http://static.guim.co.uk/sys-    images/Sport/Pix/pictures/2008/10/31/1225454147507/Mike-Tyson-001.jpg" />
<button id="Click1" onclick="changeImage();">Click to change!</button>
</body>

<br>

<script> 

function changeImage()
{
var img = document.getElementById("image2");
img.src="http://static.guim.co.uk/sys-images/Technology/Pix/pictures/2012/3/5/1330958259135/Halo-4-007.jpg";
return false;
}

</script>

<body>
<img id="image2" src="http://cdn.slashgear.com/wp-content/uploads/2012/04/halo-master- chief-1.jpg" />
<button id="Click2" onclick="changeImage();">Click to change!</button>
</body>

<br>

</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:35:28+00:00Added an answer on June 13, 2026 at 10:35 pm

    There are several issues in your page: Missing document type declaration, missing title tag, actual content HTML in head and multiple body tags. In addition to these, changeImage() is replaced in every script with a new one. Finally there will be only the last function, which changes the src for #image2.

    If I’ve understood your question correctly, you need something like this:

    The script in the head:

    function changeImage (element) {
        var n,
            imageData = [
                [
                    {
                        src: "http://thechive.files.wordpress.com/2011/10/william-defoe.jpg",
                        caption: "Caption for image 1.1"
                    },
                    {
                        src: "http://cdn.memegenerator.net/images/300x/159304.jpg",
                        caption: "Caption for image 1.2"
                    }
                ],
                [
                    {
                        src: "http://static.guim.co.uk/sys-images/Sport/Pix/pictures/2008/10/31/1225454147507/Mike-Tyson-001.jpg",
                        caption: "Caption for image 2.1"
                    },
                    {
                        src: "http://playerperspective.com/wp-content/uploads/2011/05/mike-tyson-3.jpg",
                        caption: "Caption for image 2.2"
                    }
                ],
                [
                    {
                        src: "http://cdn.slashgear.com/wp-content/uploads/2012/04/halo-master-chief-1.jpg",
                        caption: "Caption for image 3.1"
                    },
                    {
                        src: "http://static.guim.co.uk/sys-images/Technology/Pix/pictures/2012/3/5/1330958259135/Halo-4-007.jpg",
                        caption: "Caption for image 3.2"
                    }
                ]
            ];
        if (element > -1) {
            document.getElementById('image' + element).src = imageData[element][1].src;
            document.getElementById('caption' + element).innerHTML = imageData[element][1].caption;
        } else {
            for (n = 0; n < imageData.length; n++) {
                document.getElementById('image' + n).src = imageData[n][0].src;
                document.getElementById('caption' + n).innerHTML = imageData[n][0].caption;
            }
        }
        return;
    }
    

    And the body:

    <body>
        <button onclick="changeImage(-1);" >Click to restore all!</button>
        <div>
            <h1 id="caption0">Caption for image 1.1</h1>
            <button onclick="changeImage(0);">Click to change!</button>
            <img id="image0" src="http://thechive.files.wordpress.com/2011/10/william-defoe.jpg" />
        <div>
            <h1 id="caption1">Caption for image 2.1</h1>
            <button onclick="changeImage(1);">Click to change!</button>
            <img id="image1" src="http://static.guim.co.uk/sys-images/Sport/Pix/pictures/2008/10/31/1225454147507/Mike-Tyson-001.jpg" />
        </div>
        <div>
            <h1 id="caption2">Caption for image 3.1</h1>
            <button onclick="changeImage(2);">Click to change!</button>
            <img id="image2" src="http://cdn.slashgear.com/wp-content/uploads/2012/04/halo-master-chief-1.jpg" />
        </div>
    </body>
    

    As you can see, the data for paths and captions is in the objects in nested array. This way they are separated from actual content, and the code is easier to maintain.

    You didn’t provide an example of the layout for the captions of the images, so I put caption information in H1 tags above the image. Feel free to change the element and its position. Notice, that you can also use HTML-tags within caption texts.

    However, there are some limitations and waste of CPU-time when executing this code: imageData is created by every click and only two different paths can be used per element.

    I’d suggest you to have more object-oriented approach to the task. Please take a look at this JSfiddle, which demonstrates quite similar functionality, and is more flexible and maybe easier to maintain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question in here, and I would like to ask if
This is a piece of code, I am trying to read a project. I
This is driving me insane! Here's the code: public static void main(String[] strings) {
This test checks if only one SP ends with Insert, Load, or Save, then
This is mind-boggling... I can make getResource() and getResourceAsStream() work properly when I run
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This is a question purely to satisfy my own curiosity. Here in Norway it's
This is an extension of this question here: Getting Facebook Events using fql I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This question is directly related to this SO question I posed about 15 minutes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.