So I have a script that generates a hex code, and changes the background. Here's the code
$(document).ready(function() {
$(document).keydown(function(e) {
if (e.keyCode == '32') {
var color = "#" + Math.random().toString(16).slice(2, 8);
document.write(color);
document.body.style.backgroundColor = color;
}
});
});
The issue is when I press space, it only changes the color once, and I won't be able to press space again to generate another color without reloading the page. Here's a demo. Any ideas?