To better assist you, it would be beneficial to see snippets of your code in order to provide a thorough response here.
If your HTML structure resembles something like this (although steps could be omitted if there is an 'id' in your 'img' tags), the following JavaScript solution may work:
<div id="image-wrapper">
<img src="1.jpg">
<img src="2.jpg">
<img src="3.jpg">
...
</div>
With plain JavaScript, you can implement the following approach:
var rnd = ... //generate a random number
document.querySelectorAll("img").forEach(img => {
img.onclick = () => {
let src = img.src;
src = src.split("/");
src = src[src.length - 1];
src = src.split(".");
src = src[0];
src = parseInt(src);
if(rnd !== src) {
img.parentElement.removeChild(img);
} else {
alert("foo");
}
};
});
Explanation: By using document.querySelectorAll
, you can select multiple HTML nodes and iterate over them using array.forEach
. Registering an onclick
event on each image, extracting the filename from the source, comparing it to a random number, and manipulating the DOM accordingly.
I hope this guidance helps. For further assistance, please enrich your query with additional details :)