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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:05:05+00:00 2026-06-02T07:05:05+00:00

So basically what I am looking for is how to have a random image

  • 0

So basically what I am looking for is how to have a random image javascript code but the images are in two different divs but I would like the random images to come from the same array.

I plan to take a JS class this summer so I don’t have to ask anymore because I feel like this should be simple…

Currently I am just using the code from javascript kit in two different locations:

<script language="JavaScript">
<!--

/*
Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) 
Over 400+ free JavaScripts here!
Keep this notice intact please
*/

function random_imglink(){
    var myimages=new Array()
    //specify random images below. You can have as many as you wish
    myimages[1]="image1.gif"
    myimages[2]="image2.gif"
    myimages[3]="image3.gif"
    myimages[4]="image4.gif"
    myimages[5]="image5.gif"
    myimages[6]="image6.gif"

    var ry=Math.floor(Math.random()*myimages.length)
    if (ry==0)ry=1
    document.write('<img src="'+myimages[ry]+'" border=0>')
}
random_imglink()
//-->
</script>

but what I hope to achieve is:

<script language="JavaScript">
<!--

/*
Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) 
Over 400+ free JavaScripts here!
Keep this notice intact please
*/

function random_imglink(){
    var myimages=new Array()
    //specify random images below. You can have as many as you wish
    myimages[1]="image1.gif"
    myimages2[1]="image1a.gif"
    myimages[2]="image2.gif"
    myimages2[2]="image2a.gif"

    var ry=Math.floor(Math.random()*myimages.length)
    if (ry==0) ry=1
    document.write('<img src="'+myimages[ry]+'" border=0>')
}
random_imglink()
//-->
</script>

RENDERED CODE WITHIN DIVS

<div class="one"><img src="img1.gif"></div>
<div class="two"><img src="img1a.gif"></div>

REFRESHED

<div class="one"><img src="img2.gif"></div>
<div class="two"><img src="img2a.gif"></div>
  • 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-02T07:05:07+00:00Added an answer on June 2, 2026 at 7:05 am

    For what you are wanting to do, I don’t see the point to store them on arrays and do complex stuff to get them.

    If the URL paths are the same, there is no need to chose them from an array:

    function refreshImages() {
        var max = 10;
        var rand = (Math.floor(Math.random() * max) + 1); //1-10
        var src1 = "http://example.com/image" + rand + ".gif";
        var src2 = "http://example.com/image" + rand + "a.gif";
    
        document.getElementById("div1").innerHTML = "<img src='" + src1 + "' />";
        document.getElementById("div2").innerHTML = "<img src='" + src2 + "' />";
    }
    
    window.onload = function() {//need to wait until the divs are loaded
        refreshImages();
    }​
    

    and in your HTML:

    <div id="div1"></div>
    <div id="div2"></div>
    

    Note: I added +1 to rand because in your example you started at image1, if you want the possibility to start at image0, just remove the +1 and the range of possibilities will change to 0-9

    jsFiddle demo (because the images don’t exist, you need to see the source code)

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

Sidebar

Related Questions

I would like to have fuzzy looking border around my Canvas control. Basically, I
I have two tables, a_users and a_student_tutors. I am basically looking to join the
Basically, I am using Wireshark looking at captures that have been created previously. How
My team's current project involves re-writing retrieval libraries in JavaScript. We are basically looking
Basically just looking to see if you can capture an image from the webcam
Basically I'd be looking to implement a method like this. IQueryAble GetQuery<T>(Entities db) or
I have the following model class MyClass { id someRandomString } I would like
Basically i'll try and sum this up. I have a bunch of potential random
I have a component I created that works like a Viewstack but the next
i know this question is a bit subjective.. but i am basically looking to

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.