button - Javascript background changing based on function -
i have simulate function chooses random numbers between 0 , 37 when click button. if it's even, background red. if it's odd, background black. initial background light blue. have far, , not working.
<html> <head> <title>wheel</title> <script type="text/javascript"> var currentnum = wheelspins(); document.body.style.backgroundcolor = "#00ffff"; function wheelspins() { return math.floor(math.random() * 37); if(currentnum % 2 == 0){ style.backgroundcolor="#ff0000"; } else if(currentnum %2 == 1){ style.backgroundcolor="#000000"; } } </script> </head> <body> <form> <input type="text" name="number" value="" id="wheelspins()" size="10"/> <input type="button" value="spin wheel" onclick="document.getelementbyid('wheelspins()').value=wheelspins();"/> </form> </body> </html>
i see 2 possible issues. first, evaluation code never gets run because function returns before background can changed. second, should write document.body.style.background
instead of style.background
in function. change wheelspins()
function , should work:
function wheelspins() { //if return here, rest of function won't run currentnum = math.floor(math.random() * 37); if(currentnum % 2 == 0){ document.body.style.backgroundcolor="#ff0000"; } else if(currentnum %2 == 1){ document.body.style.backgroundcolor="#000000"; } return currentnum }
Comments
Post a Comment