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 6706523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:31:50+00:00 2026-05-26T07:31:50+00:00

Trying to make something simple in JS. Lets say I have two images, I

  • 0

Trying to make something simple in JS. Lets say I have two images, I want the page to load each image randomly. I want the two images to be swapped, when it gets clicked, it should change to the other image. How can I do this? Thanks

  • 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-05-26T07:31:51+00:00Added an answer on May 26, 2026 at 7:31 am

    Here’s plain javascript code (no frameworks used) that randomly loads one of the images in the imgs array as the initial image and then cycles through different images on each press of the button and a jsfiddle where you can see it work: http://jsfiddle.net/jfriend00/R7nUa/:

    var imgs = [
        "http://photos.smugmug.com/photos/344291068_HdnTo-Th.jpg",
        "http://photos.smugmug.com/photos/344290962_h6JjS-Th.jpg",
        "http://photos.smugmug.com/photos/344290515_bWVzK-Th.jpg"
    ];
    
    // select a random image from the img URL array
    function getRandomImage() {
        var index = Math.floor(Math.random() * imgs.length);
        return(imgs[index]);
    }
    
    // create image tag with random image
    function initImage() {
        var img = new Image();
        img.id = "dynImage";
        img.src = getRandomImage();
        var o = document.getElementById("imageHolder");
        o.appendChild(img);
    }
    
    // given the current URL, get the next URL in the array, 
    // wrapping back to beginning if needed
    function nextImage(current) {
        for (var i = 0; i < imgs.length; i++) {
            if (imgs[i] == current) {
                i++;
                if (i >= imgs.length) {
                    i = 0;
                }
                return(i);
            }
        }
        return(0);
    }
    
    // put a different image into the image tag
    function swapImage() {
        var o = document.getElementById("dynImage");
        var next = nextImage(o.src);
        o.src = imgs[next];
    }
    
    // initialize the button event handler
    function initButton() {
        var o = document.getElementById("swap");
        if (o.addEventListener) {
            o.addEventListener("click", swapImage);
        } else {
            o.attachEvent("onclick", swapImage);
        }
    }
    
    initImage();
    initButton();
    

    You can put as many URLs into the imgs array as you want. You can see this working here: http://jsfiddle.net/jfriend00/R7nUa/ where I’ve also added preloading so the swap is instant when you press the button.

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

Sidebar

Related Questions

I'm trying to make something very simple.. but I do wrong, and I don't
Im trying to make something similar to twitpic.com's email submission feature. Their address schema
Ok I am trying to make something to ask you random multiplication questions. Now
I'm trying to create a user control that allows users to make something like
I am trying to build a Retirement Calculator as a chance to make something
I've been trying to make a little simple game just to test my logics,
So I recently have been trying to incorporate more sub forms to make the
So I am trying to do something simple: printfn Enter a number: try let
Okay, so I'm trying to make a full text search in multiple columns, something
I'm trying to make a simple pop-up <div> that's designed to show mouse coordinates

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.