Skip to content

Instantly share code, notes, and snippets.

@CristalT
Last active July 6, 2016 00:15
Show Gist options
  • Save CristalT/310523a4ae550e3962fa80c2e9c3f875 to your computer and use it in GitHub Desktop.
Save CristalT/310523a4ae550e3962fa80c2e9c3f875 to your computer and use it in GitHub Desktop.
It allows navigate an HTML table by using keyboard arrows
$.fn.arrownav = function(options) {
var settings = $.extend({
focusOnLoad: true
}, options);
var curRow = this.children('tr:first-child'),
r=0;
if(settings.focusOnLoad) {
curRow.focus();
}
this.children('tr').click(function(){
curRow = $(this);
});
this.keydown(function(e) {
if(e.keyCode == 40) {
e.preventDefault();
r = curRow.next();
}
if(e.keyCode == 38) {
e.preventDefault();
r = curRow.prev();
}
if(r.length > 0) {
curRow = r;
curRow.focus();
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment