javascript – Navigating the site using the keyboard

Question:

It is necessary to create site navigation, for example, using the spacebar or shift + arrow keys to select the first element on the page, and if there is focus on any element, then select the next one. That is, emulate tab, only recursively navigate through all the elements of the page so that there is no transition to the browser menu.

$(document).on("keydown", function(event) {
if(event.keyCode===32){
    // тут логика
}});

Answer:

The logic (without code) is something like this:

1) Save to the array all elements that can become active

2) When you press the desired key, determine the index of the currently selected element in the array

3) Increase the index by 1, select the next element from the array.

4) If no element is selected or the last one – select the first (zero).

Scroll to Top