-1

I would like for the div to change to a random color generated by my makeColor variable every time the button is clicked. Thanks in advance.

var makeColor ="#" + Math.floor((Math.random() * 999) + 1);

$("button").click(function(){
  $("div").css("background",makeColor);
});
The Process
  • 5,913
  • 3
  • 30
  • 41
shinny_k
  • 1
  • 2

1 Answers1

2

Use a function for this task

function randomColor() {
    var c = "#";
    for (var i = 0; i < 6; i++) {
        c += (Math.random() * 16 | 0).toString(16);
    } 
    return c;
}

var a = document.getElementById("id1").style;
a.color = randomColor();
<h1 id="id1">stackoverflow</h1>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392