I am trying to make a button in Javascript which when clicked, changes the background color to a random color.
My code runs fine on the first click but doesn't work on subsequent clicks.
What can I do to fix this in pure javascript, without any jquery. Thanks!
var buton=document.getElementById("buton");
var randcol= "";
var allchar="0123456789ABCDEF";
buton.addEventListener("click",myFun);
function myFun(){
for(var i=0; i<6; i++){
randcol += allchar[Math.floor(Math.random()*16)];
}
document.body.style.backgroundColor= "#"+randcol;
}
Aucun commentaire:
Enregistrer un commentaire