Your code doesn’t work because the function which accesses the direction
variable accesses it before it is set. I’ve changed script ordering here and it works fine now: http://jsfiddle.net/bz2g5ong/7/
solved Can’t get it to work: Use value from one javascript function in another function