I am making a whack the mole game with Vanilla JS and I am trying to make the person whack whatever he wants by letting them upload an image by input file but even after days of searching for any type of solution I had no luck.
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 Vanilla:
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('');
}
The game works fine but the image uploaded does not appear:
If anyone could help me with this I will be more than welcome.
These project is part of #javascript30. I recreate them and change parts of it to practice Vanilla JS but I have been stuck for days with this one.
