-2

Hey I am new to coding and have an assignment where i need to include random colour picker for the background. The colours are red, green, yellow and blue. I have been researching and playing around with it but I have come to a stand still. Any help telling me how to do it would be greatly appreciated. Thanks :)

  • 2
    Possible duplicate of [Getting random value from an array](http://stackoverflow.com/questions/4550505/getting-random-value-from-an-array) – Adam Azad Sep 23 '16 at 02:06

2 Answers2

0

Use jQuery to target the css properties.

color = ["red", "green", "yellow", "blue"];

        var randomNum = Math.floor(Math.random() * color.length);
        console.log(randomNum);
        var randomColor = color[randomNum];
        $("body").css("background-color", randomColor);

This might help you.

pmaddi
  • 449
  • 5
  • 18
0

Here is the javascript part

var colors = ["red", "green", "yellow", "blue"]; //Array of colors
var randomColor = colors[Math.floor(Math.random() * colors.length)]; //Pick one randomly
document.body.style.background = randomColor; //Apply the color on the body background
Weedoze
  • 13,683
  • 1
  • 33
  • 63