Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
Obtener la imagen del archivo de entrada y luego agregarla como fondo a un div

Estoy haciendo un juego de golpear al topo con Vanilla JS y estoy tratando de hacer que la persona golpee lo que quiera permitiéndole cargar una imagen por archivo de entrada, pero incluso después de días de buscar cualquier tipo de solución, no tuve suerte.

HTML:

 <input type='file' id='getval' name="background-image" /> <div class="game"> <div class="hole hole1"> <img class="mole"> </div> <div class="hole hole2"> <img class="mole"> </div> </div>

Javascript vainilla:

 document.getElementById('getval').addEventListener('change', readURL); function readURL() { let file = document.getElementById("getval").files[0]; document.getElementsByClassName('mole')[0].setAttribute("style", "background-image: url(" + file + ")"); }

CSS:

 .mole { background-image: url(''); }

El juego funciona bien pero no aparece la imagen subida:

ingrese la descripción de la imagen aquí

Si alguien pudiera ayudarme con esto, sería más que bienvenido.

Este proyecto es parte de #javascript30. Los recreo y cambio partes para practicar Vanilla JS, pero he estado atascado durante días con este.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puedes usar algo como esto.

https://www.w3docs.com/learn-javascript/file-and-filereader.html

https://developer.mozilla.org/en-US/docs/Web/API/FileReader

 const fileInput = document.getElementById('imageInput'); const images = document.querySelectorAll('.mole'); fileInput.addEventListener('change', (e) =>{ const file = e.target.files[0]; let fileReader = new FileReader(); fileReader.readAsDataURL(file); fileReader.onload = function (){ images[0].setAttribute('src', fileReader.result); // images[0].setAttribute('style', `background-image: url('${fileReader.result}')`); } })
 body { min-height: 100vh; margin: 0; background-color: bisque; display: grid; place-content: center; } .box{ display: flex; flex-direction: column; place-content: center; align-items: center; } input{ margin-bottom: 1rem; } .images{ display: flex; flex-direction: row; } .mole { width: 10rem; height: 10rem; margin-bottom: 1rem; }
 <div class="box"> <input type="file" id="imageInput" /> <div class="images"> <img class="mole" /> <img class="mole" /> </div> </div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!