generate random color jquery 1

generate random color jquery

$("#btn").click(() => {
  var colorCode = "1234567890abcdef";
  var color = "";
  for (var i = 0; i < 6; i++) {
    color += colorCode.charAt(Math.floor(Math.random() * colorCode.length));
  $("#hex").html("HEX: " + "#" + color); //Display hexacode
  $("#displaycolor").css("background", "#" + color); //Display color

Here is what the above code is Doing:
1. We are creating a variable called colorCode and assigning it a string of all the possible characters that can be used in a hexacode.
2. We are creating a variable called color and assigning it an empty string.
3. We are creating a for loop that will run 6 times.
4. Inside the for loop, we are adding a random character from the colorCode string to the color variable.
5. We are displaying the hexacode in the #hex element.
6. We are changing the background color of the #displaycolor element to the hexacode.

Similar Posts