I am using Firestore for my database and looking to populate my HTML with the details I receive from it. Below is the connection and javascript method that populates my HTML:
db.collection("Ingredients2").where("Category", "==",
0).get().then(snapshot => {
snapshot.forEach(doc => {
//get database information
const price = doc.data().Price;
const name = doc.data().Name;
const id = doc.data().IngredientID;
const image = doc.data().Photo;
//create elements
var button = document.createElement("button");
button.className = "imgButton";
var divbunname = document.createElement("bunname" + id);
divbunname.className = "imgName";
var divbunimage = document.createElement("bunimage" + id);
divbunimage.className = "imgPicture";
var divbunprice = document.createElement("bunprice" + id);
divbunprice.className = "imgPrice";
var image1 = document.createElement("img" + id);
image1.className = "imgPicture img";
divbunprice.innerHTML = price;
divbunname.innerHTML = name;
image1.src = image;
document.getElementById("bunstab").appendChild(button);
button.appendChild(divbunname);
button.appendChild(divbunimage);
divbunimage.appendChild(image1);
button.appendChild(divbunprice);
});
})
.catch(err => {
console.log('Error getting documents', err);
});
This populates the name and price but the image does not appear. This is my css:
.imgButton {
position: relative;
cursor: pointer;
border: none;
background-color: rgba(119, 119, 119, 0.541);
border-radius: 25px;
width: 120px;
height: 120px;
margin-left: 30px;
margin-top: 30px;
}
.imgName {}
.imgPicture {
width: 60px;
height: 60px;
background-size: 100%;
vertical-align: middle;
display: inline-block;
outline: pink solid;
float none;
}
.imgPicture img {
width: 100%;
height: 100%;
outline: green solid;
}
Any ideas on why the image is not showing?
Here is the problem
var image1 = document.createElement("img" + id);
you are trying to add id to a native createElement() method and hence browser doesn't recognize it as an image element, for example browser is expecting something like <img></img>
tag to render image but it getting something like <img123></img123>
so images are not showing up in an unknown tag. You have to create it like
var image1 = document.createElement("img");
if you want to add id to any element, you can add it separately like
var image1.id = id;
hope it'll help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.