I need a JavaScript that will randomly display a link with two images, but the link is related to the two images, so they must appear together. For example: Text link: "Car Goes 300 MPH" w/ embedded link to buy car. [Image of car] Button w/ embedded link to buy car. See how the link and the images go together? I don't want the Script to randomly generate the links/images independent of each other. Is there a script that can do this? Thanks in advance
Untested but should work. <script type="text/javascript"> <!-- var text1 = new Array(); var text2 = new Array(); var images = new Array(); images[0] = 'http://www.url/image.jpg'; text1[0] = 'Car Goes 300 MPH'; text2[0] = 'Button '; images[1] = 'http://www.url/image.jpg'; text1[1] = 'Car Goes 300 MPH'; text2[1] = 'Button '; images[2] = 'http://www.url/image.jpg'; text1[2] = 'Car Goes 300 MPH'; text2[2] = 'Button '; var rand = Math.floor(Math.random()*images.length); document.write('<p>'+ text1[rand] +'</p>'); document.write('<p>'+ images[rand] +'</p>'); document.write('<p>'+ text2[rand] +'</p>'); //--> </script> Code (markup):
Math.floor = Rounds a fractions down Math.random() = Returns a random number between 0 and 1 *images.length); Multiplies this number by the length of the array. So all in all, it creates a random key which exists in the array so we can show a random image.