'How to show/hide a series of ordered elements by Javascript?

In a series of elements ordered by id numbers as

<div id="test-1">
<div id="test-2">
<div id="test-3">
<div id="test-4">
<div id="test-5">

Imagine that we want to show/hide each element by arrow keys. What is the best way to do so in pure JavaScript?

The question is when the first three elements are shown, how we can detect that back arrow show hide element #3 and forward arrow should show element #4?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source