Ehsan Qureshi Ehsan Qureshi - 1 year ago 90
HTML Question

Random Image with Random Links


var images_src = ["PrizeImage-1","PrizeImage-2","PrizeImage-3",];
var links = ["Participate1","Participate2","Participate3","Participate4",];

<a href="random_link" ><img src='random_img' alt='image'></a>

how can I call random links with random image in a single HTML page

Answer Source

If you want to set the image and link with random values you can do it like this:


<a href="random_link" id="linkEl"><img id='imgEl' src='random_img' alt='image'></a>


var Links = ["Link1" , "Link2" , "Link3" , "Link4"];
var images_src = ["Image1" , "Image3" , "Image3" , "Image4"];

var randomLink = Links[parseInt(Math.random()*Links.length)]; //Get random link (with random number from 0 to the link array's length
var randomImage= images_src[parseInt(Math.random()*images_src.length)];//Get random image
var linkEl = document.getElementById('linkEl'); //Get link element
var imgEl = document.getElementById('imgEl'); //Get image eement

linkEl.href = randomLink; //Set the link
imgEl.src = randomImage; //Set the image

For more funcionality, you should provide more detail, but this might get you going hopefully.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download