RIZY101
RIZY101

Reputation: 29

How to create multiple elements in different places using DOM methods

So I used a loop to create what I thought would be multiple divs in the form of w3-cards but it seems to only create one how can I fix this, and can I specify an attribute that will put it in a specific place and or make it a specific size because currently it takes up the whole width of the screen. Also this snippet is in a much larger method where eventually I will send it a value for players, and yes I do know that this loop if it worked properly would only create three cards one player is exclude because they are a judge and don't need a card. Thanks again any help is appreciated.

var players = 4;
for(r = 0; r < players; r++){
  var r = document.createElement("div");
  r.className = "w3-card-4 w3-yellow w3-center";
  document.body.appendChild(r);
  r.innerHTML = "a meme";
}

Upvotes: 1

Views: 180

Answers (1)

Bernd Strehl
Bernd Strehl

Reputation: 2920

Your loop won't run through, because your condition very likely is only fulfilled one time. r = 0; r < players; r++ because you reassign the value of r. Try another variable for the counter like this:

var players = 4;
for(i = 0; i < players; i++){
  var r = document.createElement("div");
  r.className = "w3-card-4 w3-yellow w3-center";
  document.body.appendChild(r);
  r.innerHTML = "a meme";
}

Upvotes: 2

Related Questions