Answers for "how to use the random function on images instrad of numbers in javascript"

0

random image and link js

<script type="text/javascript">
//Javascript Created by Computerhope https://www.computerhope.com/
//store the quotations in arrays
var images = [],
index = 0;
images[0] = "<a href = 'https://www.computerhope.com/'><img src='https://www.computerhope.com/banners/banner.gif' alt='Visit Computer Hope'></a>";
images[1] = "<a href = 'https://www.computerhope.com/history'><img src='https://www.computerhope.com/banners/banner2.gif' alt='Computer History'></a>";
images[2] = "<a href = 'https://www.computerhope.com/'><img src='https://www.computerhope.com/banners/banner3.gif' alt='Visit Computer Hope'></a>";
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
//done
</script>
Posted by: Guest on March-05-2020
0

how to change image dynamically by js with random number

let imageArray =new Array();

imageArray.push("images/dice1.png");
imageArray.push("images/dice2.png");
imageArray.push("images/dice3.png");
imageArray.push("images/dice4.png");
imageArray.push("images/dice5.png");
imageArray.push("images/dice6.png");


function randomGen(){
    let randomVar=Math.ceil(Math.random()*6) ;
    return randomVar;
}

document.querySelector("#dice1").lastElementChild.setAttribute("src",imageArray[randomGen()]);
Posted by: Guest on May-27-2021

Code answers related to "how to use the random function on images instrad of numbers in javascript"

Browse Popular Code Answers by Language