default img if img fails to load

This commit is contained in:
Kara Hoagland 2022-11-21 12:29:06 -08:00
parent 1db54e9f7a
commit 52bc6a27a4
2 changed files with 8 additions and 11 deletions

View File

@ -138,12 +138,11 @@ class ReviewCard extends HTMLElement {
let mealImg = document.createElement("img"); let mealImg = document.createElement("img");
mealImg.setAttribute("id", "a-mealImg"); mealImg.setAttribute("id", "a-mealImg");
mealImg.setAttribute("alt","Meal Photo Corrupted"); mealImg.setAttribute("alt","Meal Photo Corrupted");
if(data["mealImg"] != ""){ mealImg.setAttribute("src",data["mealImg"]);
mealImg.setAttribute("src",data["mealImg"]); mealImg.addEventListener("error", function(e) {
}
else{
mealImg.setAttribute("src", "./assets/images/icons/plate_with_cutlery.png"); mealImg.setAttribute("src", "./assets/images/icons/plate_with_cutlery.png");
} e.onerror = null;
});
//meal name setup //meal name setup
let mealLabel = document.createElement("label"); let mealLabel = document.createElement("label");

View File

@ -16,12 +16,11 @@ function setupInfo(){
//meal image //meal image
let mealImg = document.getElementById("d-mealImg"); let mealImg = document.getElementById("d-mealImg");
if(currReview["mealImg"] != ""){ mealImg.setAttribute("src",currReview["mealImg"]);
mealImg.setAttribute("src",currReview["mealImg"]); mealImg.addEventListener("error", function(e) {
}
else{
mealImg.setAttribute("src", "./assets/images/icons/plate_with_cutlery.png"); mealImg.setAttribute("src", "./assets/images/icons/plate_with_cutlery.png");
} e.onerror = null;
});
//meal name //meal name
let mealLabel = document.getElementById("d-mealName"); let mealLabel = document.getElementById("d-mealName");
@ -78,7 +77,6 @@ function setupUpdate(){
//Set value of each input element to current's values //Set value of each input element to current's values
document.getElementById("mealImg").defaultValue = currReview["mealImg"]; document.getElementById("mealImg").defaultValue = currReview["mealImg"];
document.getElementById("imgAlt").defaultValue = currReview["imgAlt"];
document.getElementById("mealName").defaultValue = currReview["mealName"]; document.getElementById("mealName").defaultValue = currReview["mealName"];
document.getElementById("comments").textContent = currReview["comments"]; document.getElementById("comments").textContent = currReview["comments"];
document.getElementById("s" + `${currReview["rating"]}`).checked = true; document.getElementById("s" + `${currReview["rating"]}`).checked = true;