I need to remove all event listeners when the user presses the space bar and get them back when press again the press bar or any key. How can I achieve it? My event listeners:
function makeRows(rows, cols) {
container.style.setProperty('--grid-rows', rows);
container.style.setProperty('--grid-cols', cols);
container.style.border = '3px solid #0000FF'
for (c = 0; c < (rows * cols); c++) {
let cell = document.createElement("div");
container.appendChild(cell).className = "grid-item";
cell.addEventListener("mouseover", paint);
};
};
EDIT: I have tried this to remove the event listeners:
document.addEventListener("keypress", function(){
const grid = document.getElementsByClassName('grid-item');
Array.from(grid).forEach((el) => {
el.removeEventListener("mouseover", paint);
});
});
But now I do not know how to get them back when the user presses a key again...
Thanks in advance