dimanche 27 septembre 2020

Button to change background color randomly

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
    document.body.style.backgroundColor = changeC()
});

var x = Math.floor(Math.random() *256);
    y = Math.floor(Math.random() * 256);
    z = Math.floor(Math.random() * 256);

function changeC() { 
    return  "rgb(" + x + "," + y + "," + z + ")";
};

First click changes the background color.
Consecutive clicks don't.
How to modify code so that consecutive clicks will also change the background color randomly?




Aucun commentaire:

Enregistrer un commentaire