I'm making a clicker game in which you press spacebar to increase the counter. Every time the counter goes up the next value is "stacked" on top of it, making it overlap. So if the counter was 1 and the spacebar is pressed, then the next value,(2) would overlap the 1. How do I fix this? (Please bear in mind that I'm not very well versed in coding yet, so please try to keep it as dumb as possible)
You can try something like the following:
var count = 0;
document.addEventListener("keypress", function(e) {
if (e.code === "Space") {
count++;
document.getElementById("counter").innerHTML = count;
}
});
<div>Count: <span id="counter">0</span></div>
This will replace the previous value with the new counter value everytime the space bar is pressed