-2

so I have this loader animation div and I want it to change colour every time I refresh. I already have an array for the colours if needed: ["#c764b3", "#d55f5f", "5f92d5", "#6bd1aa", "#6fd55b", "#799b00", "#ce3434"] , but nothing seems to work, and I don't know why.

Thanks in advance.

filip
  • 13
  • 5

2 Answers2

1

let colors = ["#c764b3", "#d55f5f", "#5f92d5", "#6bd1aa", "#6fd55b", "#799b00", "#ce3434"];

onload = function() {
  document.getElementById("color-div").style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
}
#color-div {
  width: 100px;
  height: 100px;
  margin: auto;
  border: 2px solid black;
  border-radius: 5px;
}
<div id="color-div"></div>
ElectricShadow
  • 683
  • 4
  • 16
0

This should do it!

`let colorArray = ["#c764b3", "#d55f5f", "5f92d5", "#6bd1aa", "#6fd55b", "#799b00", "#ce3434"];

let rc = Math.floor(Math.random() * colorArray.length);

console.log(rc);`