Answers for "javascript on spacebar press"

1

execute JS code after pressing space bar

document.body.onkeyup = function(e){
    if(e.keyCode == 32){
        //your code
    }
}
Posted by: Guest on June-19-2020
0

javascript on spacebar press

var hits = 0;
var hitElement = document.querySelector('.hits');
document.body.onkeyup = function (e) {
    if (e.keyCode == 32) {
        addHit();
    }
}

var addHit = function () {
    hits++;
    renderHits();
}

var renderHits = function () {
    hitElement.innerHTML = hits;
}

var resetHits = function () {
    hits = 0;
    renderHits();
}
Posted by: Guest on May-30-2021

Browse Popular Code Answers by Language