The default tabbing behavior is to just go to the next (in source order) form element so you could just iterate through all the elements you care about, find the one that has focus, and move the focus to the next one. We have the :input
selector for finding the form elements so something like this:
var $all = $('form :input');
var focused = $(':focus')[0];
for(var i = 0; i < $all.length - 1; ++i) {
if($all[i] != focused)
continue;
$all[i + 1].focus();
break;
}
// Must have been focused on the last one or none of them.
if(i == $all.length - 1)
$all[0].focus();
Demo: http://jsfiddle.net/ambiguous/Avugy/1/
Or you could set tabindex
attributes and increment them with wrap-around:
var next_idx = parseInt($(':focus').attr('tabindex'), 10) + 1;
var $next_input = $('form [tabindex=' + next_idx + ']');
if($next_input.length)
$next_input.focus();
else
$('form [tabindex]:first').focus();
Demo: http://jsfiddle.net/ambiguous/k9VpV/
Dealing with gaps in the tabindex
attribute values is left as an exercise.