My code involves creating an img element using JavaScript:
var photo = document.createElement("img");
This element is populated with photos whose names are stored in an array of objects. The images are loaded like this:
photo.setAttribute('src', './pics/' + myarray[id].Name + '.jpg');
Everything works perfectly except for one thing - setting a default picture for elements without specified images.
I attempted to set a default image using setAttribute
prior to the line above, hoping that it would only replace images for elements with specified pictures. However, when I checked the console.log, it still expected a 'name+.jpg' format, thus ignoring my previous line completely (which confirmed my suspicion).
So, what is the best way to assign a default picture to img elements within an array that do not have a specified image?
Thank you!