NANO85
September 15, 2024, 9:12pm
1
Ich möchte das es keine gleiche Fotos abgerufen werden.
was mache ich Falsch?
// daten vom HTML abrufen/verbinden
const imageContainerE1 = document.querySelector(".image-container");
const btnE1 = document.querySelector(".btn");
btnE1.addEventListener("click", () =>{
imageNum = 10;
addNewImages()
});
function addNewImages(){
for (let index = 0; index < imageNum; index++) {
const newImgE1 = document.createElement("img");
newImgE1.src = 'https://picsum.photos/300?random=${Math.floor(Math.random() * 2000)}';
imageContainerE1.appendChild(newImgE1);
}
}
Hallo NANO85
Dein Post ist sehr unverständlich. Vielleicht kannst du es in (``) Backticks schreiben und es wird übersichtlicher
LG
lasjorg
September 16, 2024, 7:43pm
3
Not sure if you are actually missing it, but this string needs to be a template literal using backticks.
newImgE1.src = `https://picsum.photos/300?random=${Math.floor(Math.random() * 2000)}'`;
Other than that, your code works for me.
But I would suggest you do not use imageNum
the way you are, as that is creating an implicit global. Give the addNewImages
function a parameter and pass it the number when you call it, then use the parameter inside the function.
addNewImages(10);
function addNewImages(imageNum) {
}