Skip to main content

Moving through tabular forms using the Up/Down arrow keys

Today there was a question on the Oracle APEX forum about How to Navigate through the rows of Report using up or down key. This question pop ups now and then and I decided to figure out how to do this. The solution is quite straight forward:

Set the Element Attributes of the columns of the tabular form to :
onkeyup="moveUpDown(this, event)"
and create a that function in the HTML Header:

function moveUpDown(pThis, pEvent){
var keynum;
var current = document.getElementsByName( pThis.name );
if(window.event) // IE
{ keynum = pEvent.keyCode; }
else if(pEvent.which) // Netscape/Firefox/Opera
{ keynum = pEvent.which; }
if (keynum == 40 || keynum == 38) // Key-Up or Key-Down
{ for (i=0;i < current.length;i++)
{ if ( current[i].id == pThis.id ) // This is current row
{ if (keynum == 40) // Move down
{ current[Math.min(current.length - 1,i+1)].focus(); }
else // Move up
{ current[Math.max(0,i-1)].focus(); }
}
}
}
}
Check out the live example.
PS: Changed the code a little on Dec 18 to prevent javascript errors when pressing Up on the first row and pressing Down on the last row.

Comments

Stew said…
This comment has been removed by the author.
Stew said…
Very cool! I remember reading a question about that but never noticed it got answered. Thanks for the simple implementation.

I just tried it on a tabular form that wasn't in production yet and it worked like a charm.

Thanks!
Michael A. Rife said…
This is awesome!! The code you list on the "Live Example" is missing part of the line #10 (for...) The code in your blog works fine.
Louis-Guillaume said…
Roel,

Have you tried Keynav (jQuery Plugin)?

http://plugins.jquery.com/project/keynav

I'll try to redo your demo using this tool.
CarrotCelleryOnion said…
Excellent example