I am trying to create button that changes the color randomly of my HTML header. How can I do that using jQuery?
Asked
Active
Viewed 42 times
-4
-
So what did you try? We are not here to code it for you. – epascarello May 24 '18 at 18:37
-
What have you tried so far? – Albeis May 24 '18 at 18:37
-
Welcome to StackOverflow, please read https://stackoverflow.com/help/how-to-ask to form a good question. – Nikolaus May 24 '18 at 18:46
1 Answers
-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