Skip to content

Instantly share code, notes, and snippets.

@krcourville
Created November 4, 2013 21:06
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save krcourville/7309218 to your computer and use it in GitHub Desktop.
Save krcourville/7309218 to your computer and use it in GitHub Desktop.
Navigate table with arrow keys using jQuery
$('table.arrow-nav').keydown(function(e){
var $table = $(this);
var $active = $('input:focus,select:focus',$table);
var $next = null;
var focusableQuery = 'input:visible,select:visible,textarea:visible';
var position = parseInt( $active.closest('td').index()) + 1;
console.log('position :',position);
switch(e.keyCode){
case 37: // <Left>
$next = $active.parent('td').prev().find(focusableQuery);
break;
case 38: // <Up>
$next = $active
.closest('tr')
.prev()
.find('td:nth-child(' + position + ')')
.find(focusableQuery)
;
break;
case 39: // <Right>
$next = $active.closest('td').next().find(focusableQuery);
break;
case 40: // <Down>
$next = $active
.closest('tr')
.next()
.find('td:nth-child(' + position + ')')
.find(focusableQuery)
;
break;
}
if($next && $next.length)
{
$next.focus();
}
});
@kodape
Copy link

kodape commented Jan 16, 2019

i need solution in javascript .plz help

@Roy9I91
Copy link

Roy9I91 commented Feb 22, 2019

i need solution in javascript .plz help

The fallowing link may help you.
https://stackoverflow.com/questions/4609405/set-focus-after-last-character-in-text-box

@krycek242
Copy link

Hi!, first of all thanks for this solution,
I was trying to skip readonly inputs by adding input:not([readonly] in focusableQuery but it is not working
is there a way to skip readonly inputs?

thanks

@DSBRBrasil
Copy link

It's worked ....
thks thks so much !!!!!!

@mnadeemijaz
Copy link

Thank you so much.
It is working fine

@lol97
Copy link

lol97 commented Aug 9, 2022

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment