javascript - Display random image with fitting link -


i'm using below code show random images works fine. how can add code each image gets it's own link? i.e. if "knife.png" randomly picked want have link takes user "products/knife.html" , on.

thanks help!

<div id="box"> <img id="image" /></div>  <script type='text/javascript'>   var images = [ "images/knife.png", "images/fork.png", "images/spoon.png", "images/chopsticks.png",];  function randimg() { var size = images.length var x = math.floor(size * math.random()) document.getelementbyid('image').src = images[x]; }  randimg(); 

generalize list of images that can multi-purposed - can add additional information later. surround image anchor tag (<a>) , use following.

<div id="box">   <a name="imagelink"><img id="image" /></a> </div>  <script type='text/javascript'>    var images = ["knife","fork","spoon","chopsticks",];    function randimg() {     var size = images.length     var x = math.floor(size * math.random())     document.getelementbyid('image').src = "images/"+images[x]+".png";     document.getelementbyid('imagelink').href="products/"+images[x]+".html";   } </script>  randimg(); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -