By default, the tab behavior is to simply go to the next element in the form (in the original order) so that you can simply iterate over all the elements you need, find the one that has focus, and move the focus to the next. We have :input selector to search for 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 can set the tabindex attributes and increase 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/
Working with spaces in the tabindex attribute tabindex remains as an exercise.
mu is too short Sep 05 2018-11-11T00: 00Z
source share