I have an array of images with class="btn". When clicked, the listener replace the btn.src with an image randomly from images arr.
Then, I need to have the randomly-selected-item.src stored in an array for further operations. I was able to do the random insertion of image to btn class but dont know how to grab the randomly-selected item each time and push them in an array for further operation.
In short I am trying to check if the two selected items have the same src or not.
var x = document.getElementsByClassName('btn');
for (var i = 0; i < x.length; i++) {
x[i].addEventListener('click', feedImages);
}
function feedImages(e) {
var images = ["image/1.png",
"image/2.png",
"image/3.png",
"image/4.png",
"image/5.png",
"image/6.png"];
var img = document.createElement("img");
img.src = images[Math.floor(Math.random()*images.length)];
document.getElementsByClassName("result").appendChild(img);
};
// how to grab this.img.src and push them in a new array each time so I will have something like
var newArr["selectedImage.png", ....];
after user clicked on any item
the HTML looks simple. Upon trigger SRC changes randomly to a new image.
<img class="btn" src="btn.png">
<img class="btn" src="btn.png">
<img class="btn" src="btn.png">
<img class="btn" src="btn.png">
<img class="btn" src="btn.png">