Skip to content

Instantly share code, notes, and snippets.

@fahdi
Created January 26, 2023 03:16
Show Gist options
  • Save fahdi/aeae17b5ad51d2bcb186386fc25766f9 to your computer and use it in GitHub Desktop.
Save fahdi/aeae17b5ad51d2bcb186386fc25766f9 to your computer and use it in GitHub Desktop.
BG Changer JS events example
<button onclick="changeColor()">Repaint!</button>
function getRandomColor() {
let letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function changeColor() {
let newColor = getRandomColor();
document.body.style.backgroundColor = newColor;
}
button {
display: block;
margin-left: auto;
margin-right: auto;
width: 25%;
margin-top: 10%;
font-size: 16px;
padding: 5px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment