Creating multiple image boxes within a span element without a predetermined src is the task I have set for myself in JavaScript, specifically for a basic game of blackjack. Below is the current HTML code for the span of image boxes:
Note: This site is not being hosted; it is running in my browser with no JQuery.
<span> <!-- Players Cards -->
<img class='imagebox' id='imagebox1' src=''></img>
<img class='imagebox' id='imagebox2' src=''></img>
<img class='imagebox' id='imagebox3' src=''></img>
<img class='imagebox' id='imagebox4' src=''></img>
</span>
When a card is selected in the JavaScript file, interpolation is used to display an image by changing the image's src:
function deal(box) {
let card = randomcard();
const image = document.getElementById(`imagebox${box}`);
if (image.src !== card) {
image.src = `cardpng/${card}`;
} else {
redeal();
}
}
The issue is that until a src is assigned to each image box, it will display an "image not found" icon, which I find aesthetically unpleasing. I attempted to remove this icon using the following code:
<img class='imagebox' id='imagebox1' onerror='this.style.display = "none"' src=''></img>
However, even after a src is defined and there should be no error, the style remains as "none." I am looking for a more elegant solution that requires minimal interaction between HTML and JS.
Thank you in advance for any suggestions.