vendredi 19 avril 2019

Random Image Generator not finding image in directory

I'm working on a very simple webpage that contains just a button that generates a random image above it.

I've searched online but none of the answers seem to solve my problem.

I have a js code that seems to be working: it generates a random number but when I try to link the number to '.jpg' (That's how all my images are named, ex: 1.jpg, 2.jpg, etc) it says

 " Failed to load resource:  [x].jpg:1 
    net::ERR_FILE_NOT_FOUND "

it basically means that the function I have isn't adding the random number in the variable to the file name src.

I am sure that the name of the images are [number].jpg and that the directory is correct;

I'm sorry if it sounds confusing, english is not my first language.

HTML:

     <body>
          <div class="container-fluid">
              <section class="row">
                  <div class="col-12" id="divImage"> </div>
              </section>
              <section class="row">
                  <button onclick="generator()"> Go </button>
              </section>
            </div>
          <script src="teste.js"></script>
      </body>


JS:

function generator() {
    var x= Math.floor((Math.random()*12)+1);
    console.log(x);
    document.getElementById('divImage').innerHTML='<img src="img/[x].jpg">';
}





Aucun commentaire:

Enregistrer un commentaire