-4

I am trying to create button that changes the color randomly of my HTML header. How can I do that using jQuery?

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

-3

E.g you could adapt this https://stackoverflow.com/a/1484514/3207478 on your button click event with JQuery or native JavaScript.

<button onclick="myFunction()">Change color of header</button>

<script>
function getRandomColor() {
   var letters = '0123456789ABCDEF';
   var color = '#';
   for (var i = 0; i < 6; i++) {
     color += letters[Math.floor(Math.random() * 16)];
   }
return color;
}

function myFunction() {
    var myRandomColor = getRandomColor();
    document.getElementById("myHeaderElementId").style.backgroundColor = myRandomColor;
}
</script>

next time google or use the search ;)

gco
  • 1,670
  • 7
  • 24
  • 46