samedi 10 mars 2018

Javscript: random image generator

The function is clicking on a button where the image changes to a new image. But the image doesn't seem to change when I click the button. Also the images are random. The problem I am having is how the images are uploaded on the html document and how this random image generator can work. Any help would be appreciated.

The code:

<!DOCTYPE html>
<html>
<head>
<img src="1.jpg" id="randomimg" width=200 height=200>
<button type="button" onClick="randImage()">

  <script>
  new Image().src = "1.jpg";
  new Image().src = "2.jpg";
  new Image().src = "3.jpg";

  var imgs = [
          "1.jpg",
          "2.jpg",
          "3.jpg"
         ];

  var random = function randImage(){
    Math.floor(Math.random() * imgs.length)
  };

    document.getElementById("randomimg").innerHTML = var random;
  </script>

</head>
<body>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire