-Random Array gives an Image based on div onclick event?

I was wondering how to make an array for any number of images, that when tied to an onclick event, randomly choices which image to display?

Am also wondering if an array is the best choice for this type of procedure?

Any help is greatly appreciated, thanks in advance!
t
LVL 1
toddkellerAsked:
Who is Participating?
 
basicinstinctConnect With a Mentor Commented:
Ok, I think I know what you mean.  Will this do the job for you?  If not we can tweak it.  Basically you pass the ID of the DIV where you want the image to appear.  The code expects that the DIV is otherwise empty and contains no other elements.

By the way, I didn't test this in IE (because I'm using Linux) but it worked fine in Firefox.  You should try it on IE.


<html>
<head>
<title>New Page 1</title>
<script language="JavaScript">

function getRndImg(targetDivId)
{
     var imgDir = "images/"
     var images = new Array("dog.jpg","cat.jpg","owl.jpg","flower.gif","alien.jpg");
     var newImgSrc = imgDir + images[rnd(images.length -1)];
     var tgt = document.getElementById(targetDivId);
     var newImg = document.createElement('img');
     newImg.src = newImgSrc;
     if(tgt.childNodes.length > 0)
     {
           tgt.replaceChild(newImg, tgt.childNodes[0]);
     }
     else
     {
      tgt.appendChild(newImg);
     }
}

function rnd(max)
{
     var rnd = Math.round(max * Math.random());
     return rnd;
}
</script>
</head>
<body>
<img src="images/default.jpg" onClick="getRndImg('gimmeimg');">
<div id="gimmeimg"></div>
</body>
</html>
0
 
basicinstinctCommented:
Well I don't know why the array would need to handle "any number of images" - have I missed something? It's not like you will be filling it dynamically from a database or anything, you will always know how many images are in it because you will have to type the array initialisation yourself.


Here's an example of how it could be done (note, you will need to change the image names in the array - probably also the directory path too.


<html>
<head>
<title>New Page 1</title>
<script language="JavaScript">

function getRndImg()
{
      var imgDir = "images/"
      var images = new Array("dog.jpg","cat.jpg","owl.jpg","flower.gif","alien.jpg");
      var newImg = imgDir + images[rnd(images.length -1)];
      return newImg;
}

function rnd(max)
{
      var rnd = Math.round(max * Math.random());
      return rnd;
}
</script>
</head>
<body>
<img src="images/default.jpg" onClick="this.src = getRndImg();">
</body>
</html>
0
 
toddkellerAuthor Commented:
I think i just worded the sentence weird above, sorry aobut that "any number of images" comment.

In your code above ---if i was going to have the src image just be a rollover, and use it to call the rndimg's into a div somewhere, and the images could be any size?- is there a workaround for that?..maybe just use a this.div or something to put the rndimages into the div i want?
0
 
toddkellerAuthor Commented:
Works great, thanks for the help.
t
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.