   [web] javascript - function not working
  Hey all,I'm starting to learn &#106avascript, well at least i thought i was learning it :D. I created this script where you can move a square inside a box and thats all working and all. So i thought lets make a game from it and i added the function "game()". But whenever the statement becomes true nothing happens. I though i finally started figuring out how things work in &#106avascript but i guess thats not true :D.<html><head><title>Super squareman</title><style>#gamebox {margin:auto;border: 1px solid black;width:512px;height:512px;background-color: rgb(192,192,192);}#squareman {position:relative;top: 240px;left: 240px;width:32px;height:32px;background-color:#900;border: 1px solid black;}</style><script language="&#106avascript">//instance squaremanvar squareman;var timer;var squaremanTop = 240;var squaremanLeft = 240;function init(){squareman = document.getElementById('squareman');document.onkeydown = keyListener;game();}//key detection functionfunction keyListener(e){if(!e){e = window.event;}if (e.keyCode==37 && squaremanLeft > 0){squaremanLeft -= 4;squareman.style.left = squaremanLeft + 'px';}if (e.keyCode==39 && squaremanLeft < 480){squaremanLeft += 4;squareman.style.left = squaremanLeft + 'px';}if (e.keyCode==38 && squaremanTop > 0){squaremanTop -= 4;squareman.style.top = squaremanTop + 'px';}if (e.keyCode==40 && squaremanTop < 480){squaremanTop += 4;squareman.style.top = squaremanTop + 'px';}}function game(){if (squaremanTop > 350){gamebox.style.backgroundColor = 'rgb(128,0,0)';squareman.style.backgroundColor = 'rgb(0,128,0)';}}</script></head><body onload="init()"><div id="gamebox"><div id="squareman"> </body></html>   
Make sure your divs have a closing tag: <div id="id"></div>I assume you want the color to change whenever squareman's vertical coordinate is higher than 350? Currently you only check this once, from the init function. At that point, squaremanTop still has its initial value of 240. Perhaps you meant to check for it continuously, or at least after every key press. In that case, you might move the call to game() to the keyListener function.;  Second, game() is never actually called after a key is pressed, only at the start in the init function.   ;  Hey thanks, it's working now in IE and opera but it doesn't work in Firefox wich is the browser i'm using to debug with firebug.Whats different about firefox? and what should i change in my code to make it work with firefox?[Edited by - menyo on May 26, 2010 4:04:55 AM];  Hey guys,I fixed my problem with the style change. Instead of usingQuote:gamebox.style.backgroundColor = 'rgb(128,0,0)';squareman.style.backgroundColor = 'rgb(0,128,0)';I had to use:Quote:document.getElementById('gamebox').style.backgroundColor = 'rgb(128,0,0)';document.getElementById('squareman').style.backgroundColor = 'rgb(0,128,0)';The last code correctly referenced to the CSS element, or so i was told :D.   