How to create containers all at the same time with random colours?

Tolu :

my following code essentially creates a number of boxes according to the input of the user.
However i want to ensure that when they are created, they all have a random colour.

I also want to ensure that for each new box created, it sets the margin-left of the box to be double the previous box’s margin (where the first box has margin-left 5px).

I have no idea how to do this!

function getNewRandomColor()
{
  var myArray = ['red', 'green', 'blue'];    
  var rand = myArray[Math.floor(Math.random() * myArray.length)];
  document.getElementById("container").style.backgroundColor = rand;
}

var empty = true;

function setup()
{
  if (!empty)
  {
    remove();
  }

  size = document.getElementById("input").value

  var container = document.getElementById("container");

  // create boxes
  for (var i = 0; i < size; i++)
  {
    // Create a box
    var box = document.createElement("div");

    // insert number
    box.innerHTML = i+1;

    // Add into the document
    container.appendChild(box);
    empty = false;
  }    
}

function remove()
{
  // Get all the generated boxes
  var boxes = document.getElementById("container").children;

  // Iterate through all boxes
  for (var x = 0; x < boxes.length;) 
  {
    var bA = boxes[x];
    bA.remove();
  } 
}
body {
  font-family:sans-serif;
  font-weight:700;
  font-size:15pt;
} 
#container div, div.selected {
  width: 2em;
  height: 1.5em;
  float: left;
  padding: 1em;
  margin: 1vw;
  padding-top:1.5em;
  text-align: center;
  transition: background 1s;
}
div, .unselected {
  background: #a0aeef;
}
<input type="number" name="input" id="input">

<button onclick="setup();">Draw</button>
<button onclick="remove();">Remove</button>

<div id="container"></div>

G-Cyrillus :

You could only retrieve the array key and use it as a classname, style on the fly can be applied once the element is part of the dom of the document :

ex

function getNewRandomColor(e)
{
  var myArray = ['red', 'green', 'blue'];    
  var rand = myArray[Math.floor(Math.random() * myArray.length)];
  return rand;
}

var empty = true;

function setup()
{
  if (!empty)
  {
    remove();
  }

  size = document.getElementById("input").value

  var container = document.getElementById("container");
 
  // create boxes
  for (var i = 0; i < size; i++)
  {
    // Create a box
    var box = document.createElement("div");
    var rdmClass= getNewRandomColor();
    box.setAttribute("class",rdmClass);
    marginVar = 10*i;
    box.setAttribute("style","--margR:" + marginVar  + ";border:solid/* here you bg with hex value to be updated " );
    // insert number
    box.innerHTML = i+1;
    // Add into the document
    container.appendChild(box);
    empty = false;
  }    
}

function remove()
{
  // Get all the generated boxes
  var boxes = document.getElementById("container").children;

  // Iterate through all boxes
  for (var x = 0; x < boxes.length;) 
  {
    var bA = boxes[x];
    bA.remove();
  } 
}
body {
  font-family:sans-serif;
  font-weight:700;
  font-size:15pt;
} 
#container div, div.selected {
  width: 2em;
  height: 1.5em;
  float: left;
  padding: 1em;
  margin: 1vw;
  padding-top:1.5em;
  text-align: center;
  transition: background 1s;
}
div, .unselected {
  background: #a0aeef;
}

#container div{
  margin-right:calc(var(--margR)*1px); /* updated from the style attribute added from js loop */
}

.red {background:red;}
.green {background:green;}
.blue {background:blue;}
<input type="number" name="input" id="input">

<button onclick="setup();">Draw</button>
<button onclick="remove();">Remove</button>

<div id="container"></div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19936&siteId=1