Jump to content

2 corresponding random images


Recommended Posts

i've fallen in on the deep end of javascript on my first day of ever looking at the code.. which could be a good thing.. forced to either sink or swim!I need to get a random image up.. which has been easy (lots of examples) but i have two images on the page which need to correspond with each other. Come up together but randomly..Would i..use the If statement? I've been trying this but must not be getting the wording right.or.. what?thanks

Link to comment
Share on other sites

I'm guessing the tutorials you've looked at have done something like:

var myImages = new Array();myImages[0] = "http://....image.gif";myImages[1] = "http://....image2.gif";var randImage = Math.floor( Math.random() * myImages.length);document.write('<img src="' + myImages[randImage] + '" />');

Yes?Well all you need to do is alter the array slightly, and the final output :)

var myImages = new Array();myImages[0] = new Array( "http://....image.gif" , "http://....image2.gif" );myImages[1] = new Array( "http://....image3.gif" , "http://....image4.gif" );var randImage = Math.floor( Math.random() * myImages.length);document.write('Image 1: <img src="' + myImages[randImage][0] + '" /> Image 2: <img src="' + myImages[randImage][1] + '" />');

Image.gif and Image2.gif always get shown together, and Image3.gif and Image4.gif always get shown together too.Hope that helps!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...