I am trying to make a button in Javascript that when clicked changes the background color to a random color.
My code works fine on the first click, but doesn't work on subsequent clicks.
What can I do to fix this in pure javascript without 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; }
source share