Created
November 4, 2013 21:06
-
-
Save krcourville/7309218 to your computer and use it in GitHub Desktop.
Navigate table with arrow keys using jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('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(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much.
It is working fine