Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Adding Images from a image folder dynamically to a div box

I'm trying to create a random number generator where based on the number an image is chosen from the img folder that is on the same hierarchy as the CSS folder. However the image is being chosen and being displayed.

<script type="text/javascript">

  var addObj = {

    add: function () {
      var box = document.getElementById('box');
      var box1 = document.getElementById('box1');
      var box2 = document.getElementById('box2');
      var pic = document.getElementById('pic')
      var x = Math.floor(Math.random() * 18) + 1;
      var y = Math.floor(Math.random() * 18) + 1;
      var z = Math.floor(Math.random() * 18) + 1;
      console.log(x);
      console.log(y);
      console.log(z);
    },

    EventListners: function () {
      var button = document.getElementById('rune');
      button.addEventListener('click', addObj.add, false);

      if (addObj.add.x = 1) {
        addObj.add.box = "url('./img/fa.jpg')";
      }else {
        console.log("n/a")
      }
    }

  };

addObj.EventListners();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use

  var box = document.getElementById('box');
  var box1 = document.getElementById('box1');
  var box2 = document.getElementById('box2');
  var pic = document.getElementById('pic')
  // this elements you can get globally
  var addObj = {

    add: function () {
      let x = Math.floor(Math.random() * 18) + 1;
      let y = Math.floor(Math.random() * 18) + 1;
      let z = Math.floor(Math.random() * 18) + 1;
      if (x == 1) { // the checker block you should add to the function at the eventlistener, the `=` is the assignment operator, the logically operator the `==` or the `===`
      box.src = './img/fa.jpg'; // the `box` variable is an image element, you can set the source with the `src` attribute
      }else {
        console.log("n/a")
      }
    },
    EventListners: function () {
      var button = document.getElementById('rune');
      button.addEventListener('click', addObj.add, false);

    }

  };

addObj.EventListners();

The OOP is very nice thing, but at first you should know the language well.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda