Created
September 13, 2021 06:59
-
-
Save zhibirc/db702941e13543545706335fa18e73ac to your computer and use it in GitHub Desktop.
Easy switch focus between two buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const buttons = [...document.getElementsByClassName('button')]; | |
let focusedButtonIndex = 0; | |
buttons[focusedButtonIndex].focus(); | |
window.addEventListener('keydown', event => { | |
if ( buttons.some(button => button === event.target) ) { | |
if ( event.code === 'ArrowDown' || event.code === 'ArrowUp' ) { | |
buttons[focusedButtonIndex ^= 1].focus(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment