HTML code
<button id="btn">
Click me
</button>
<span id="color" style="background-color: #fff"></span>
JS Code
let colors = ['0', '1', '2', '3', '4','5','6','7','8','9','A','B','C','D','E','F'];
let btn = document.getElementById('btn');
let colorCode = document.getElementById('color');
btn.addEventListener('click', function(){
let hex = '#';
for(let i = 0; i < 6; i++){
hex += colors[randomNumber()];
}
document.body.style.backgroundColor = hex;
colorCode.textContent = hex;
});
function randomNumber(){
return Math.floor(Math.random()*colors.length);
}
0 comments:
Post a Comment