0

Can arrow key navigation be done using just JS alone?

Not third party like superfish,etc.

mircea .
  • 241
  • 5
  • 15
  • Check out http://stackoverflow.com/questions/2217553/detecting-arrow-key-press-in-ie-via-javascript-jquery it gives examples of how to detect arrowkey keypress. – kamui Nov 21 '11 at 09:05

2 Answers2

1

Yes, here is a skeleton:

document.onkeydown = function(evt) 
{
    evt = evt || window.event;
    if (evt.keyCode == 37) 
    {
        //do something
    }
    else if (evt.keyCode == 38) 
    {
        //do something
    }
    else if (evt.keyCode == 39) 
    {
        //do something
    }
    else if (evt.keyCode == 40) 
    {
        //do something
    }
};  
MOleYArd
  • 1,258
  • 1
  • 12
  • 16
1

You might be able to get away with this:

document.onkeypress = keyHandler;

function keyHandler(evt)
{
    var evt  = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

    if (evt.keyCode == 37 /*left arrow*/) {
        //...
    }
    if (evt.keyCode == 38 /*up arrow*/) {
        //...
    }
    if (evt.keyCode == 39 /*right arrow*/) {
        //...
    }
    if (evt.keyCode == 40 /*down arrow*/) {
        //...
    }
}

Of course, there are some quirks...

Matt Montag
  • 7,105
  • 8
  • 41
  • 47
  • Ugh, those quirks are a killer. I once thought that you could build your own textbox in JavaScript, but when I saw that in Opera you cannot distinguish between, say, `Delete` and `.`... – Vilx- Nov 21 '11 at 09:18
  • how would i move the pointer to the selected list item up and down? – chovy Dec 07 '19 at 01:44