Ad

How Can I Make A Different InnerText For Every DIV Created In JavaScript?

- 1 answer

I have one small problem in JS. I need to create around seven DIVs with a for function, but each div's content (here is innerText) must be different. Is there any way to do it with a function, without modifying each and every div by hand? If I put it inside the for function, the innerText will be the same. Below is my code:

for (let i = 0; i < 7; i++) {
      const innerDiv = document.createElement("div");
      innerDiv.id = "innerDivID";
      innerDiv.classList.add("innerDivClass");
      innerDiv.innerText = 1;
      outsideCaroussel.appendChild(innerDiv);
}

Note: outsideCaroussel is another div inside the Wrapper.

Ad

Answer

At least you should put i to the div as a text:

for (let i = 0; i < 7; i++) {
      const innerDiv = document.createElement("div");
      innerDiv.id = "innerDivID";
      innerDiv.classList.add("innerDivClass");
      innerDiv.innerText = i;
      outsideCaroussel.appendChild(innerDiv);
}
Ad
source: stackoverflow.com
Ad