Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
Agregar imágenes desde una carpeta de imágenes dinámicamente a un cuadro div

Estoy tratando de crear un generador de números aleatorios donde, según el número, se elige una imagen de la carpeta img que está en la misma jerarquía que la carpeta CSS. Sin embargo, la imagen se elige y se muestra.

 <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 Respuestas
Responde la pregunta

0

Puedes usar

 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();

La programación orientada a objetos es algo muy agradable, pero al principio debes conocer bien el idioma.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda