I have three buttons and each one has an image with an empty src
. I'm trying to randomly change the image src
when clicking each button and if I'm seeing this correctly the console tells me that the image is being generated when I click but I can't seem to have it shown on the browser and I don't really get why.
const images =
["https://i.picsum.photos/id/102/60/60.jpg","https://i.picsum.photos/id/1024/60/60.jpg",
"https://i.picsum.photos/id/1062/60/60.jpg","https://i.picsum.photos/id/1069/60/60.jpg"];
const buttons = document.querySelectorAll("button");
buttons.forEach(function(el){ el.addEventListener("click", card); })
function card(){
let random = Math.floor( Math.random() * 9);
let image = this.children;
console.log(image);
image.src = images[random];
console.log(image.src); }
<div>
<button type="button"><img src="" alt=""></button>
<button type="button"><img src="" alt=""></button>
<button type="button"><img src="" alt=""></button>
</div>