Method to exchange JavaScript / html images

-1

I am having to make my site change certain images, I created the following code, somebody could accidentally point to errors ..?

<script type="text/javascript">

                    function ImgsRandom(){

                    var trocar=new Array()

                    trocar[0]='<img src="site.projeto/Logo_pilates.jpg" alt=""/>';

                    trocar[1]='<img src="site.projeto/equipamento.jpg" alt=""/>';

                    trocar[2]='<img src="site.projeto/equipamento_02.jpg" alt=""/>';

                    var whichtrocar=Math.floor(Math.random()*(trocar.length));

                    document.getElementById("imgsrandon").innerHTML = trocar[whichquote];

                    }

                    window.onload=ImgsRandom;

                    </script>
    
asked by anonymous 03.09.2017 / 17:13

2 answers

0

Well, the error you're trying to find is in trocar[whichquote] change by trocar[whichtrocar] . And start analyzing the errors with the browser with CTRL+SHIFT+I and click on the Console tab there will appear the errors!

    
03.09.2017 / 18:27
0

When you access the value of the list of images you are using the variable thatquote that until then does not exist, only changing to whichrocar will theoretically already work.

Here is an example with just one change, instead of creating an img tag I change the src attribute of an existing image.

  function ImgsRandom()
  {
      // lista de imagens
      var trocar = [
      'http://www.drodd.com/images15/1-4.png',
      'http://www.clker.com/cliparts/f/j/9/x/c/z/blue-rounded-square-with-number-2-md.png',
      'http://ghzwireless.com/live/files/No3-big.png' ];

      // criando valor randômico
      var whichtrocar = Math.floor(Math.random() * (trocar.length));

      // alterando o atributo 'src' da imagem
      document.getElementById("imgsrandon").src = trocar[whichtrocar];
  }

  window.onload = ImgsRandom;
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>

<body>
    
    <img id="imgsrandon" />
    
</body>
</html>
    
05.09.2017 / 05:34