With jQuery you could bind a keypress
event to the window, and show the next list item every time a key is pressed:
var curIndex = 0;
$(window).keypress(function() {
$("li").eq(curIndex).show();
curIndex++;
});
To make this work with only the arrow keys, a slight change is required (you need to use keydown
instead of keypress
):
var curIndex = 0;
$(window).keydown(function(e) {
if(e.keyCode === 37) {
if(curIndex > 0) curIndex--;
$("li").eq(curIndex).hide();
}
else if(e.keyCode === 39) {
$("li").eq(curIndex).show();
if(curIndex < 3) curIndex++;
}
});
Your HTML list will look something like this:
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
The list items will need to be hidden by default, using something like li { display:none }
in your CSS.
Also note that the above example does not handle the case when the last element is shown - what were you intending to happen in this case?
Here is an example fiddle showing this in action (you need to click on the "Result" frame to give it focus, then press any key to trigger the event handler).