const clickOnElementBinding = selector => { return () => { const elem = document.querySelector(selector); if(elem) elem.click(); }; }; const keybindings = { "ArrowLeft": clickOnElementBinding("#next"), "ArrowRight": clickOnElementBinding("#prev"), "r": clickOnElementBinding("#random") }; (() => { document.addEventListener("keydown", e => { if(e.key in keybindings) { e.preventDefault(); keybindings[e.key](); } }); })();