I am trying to move the focus to the next element in the tab sequence based upon the current element which has focus. Thus far I have not turned up anything in my searches.
function OnFocusOut()
{
var currentElement = $get(currentElementId); // ID set by OnFocusIn
currentElementId = "";
currentElement.nextElementByTabIndex.focus();
}
Of course the nextElementByTabIndex is the key part for this to work. How do I find the next element in the tab sequence? The solution would need to be based using JScript and not something like JQuery.
Here's something I build for this purpose:
Features:
As mentioned in a comment above, I don't think that any browsers expose tab order information. Here a simplified approximation of what the browser does to get the next element in tab order:
This only considers some tags and ignores
tabindex
attribute but might be enough depending on what you are trying to achieve.Hope this is helpful.
then use simple javascript
It seems that you can check the
tabIndex
property of an element to determine if it is focusable. An element that is not focusable has atabindex
of "-1".Then you just need to know the rules for tab stops:
tabIndex="1"
has the highest priorty.tabIndex="2"
has the next highest priority.tabIndex="3"
is next, and so on.tabIndex="0"
(or tabbable by default) has the lowest priority.tabIndex="-1"
(or not tabbable by default) does not act as a tab stop.Here is an example of how to build the list of tab stops, in sequence, using pure Javascript:
We first walk the DOM, collecting up all tab stops in sequence with their index. We then assemble the final list. Notice that we add the items with
tabIndex="0"
at the very end of the list, after the items with atabIndex
of 1, 2, 3, etc.For a fully working example, where you can tab around using the "enter" key, check out this fiddle.
I've never implemented this, but I've looked into a similar problem, and here's what I would try.
Try this first
First, I would see if you could simply fire a
keypress
event for the Tab key on the element that currently has focus. There may be a different way of doing this for different browsers.If that doesn't work, you'll have to work harder…
Referencing the jQuery implementation, you must:
1. Listen for Tab and Shift+Tab
Listening for Tab and Shift+Tab are probably well-covered elsewhere on the web, so I'll skip that part.
2. Know which elements are tab-able
Knowing which elements are tab-able is trickier. Basically, an element is tab-able if it is focusable and does not have the attribute
tabindex="-1"
set. So then we must ask which elements are focusable. The following elements are focusable:input
,select
,textarea
,button
, andobject
elements that aren't disabled.a
andarea
elements that have anhref
or have a numerical value fortabindex
set.tabindex
set.Furthermore, an element is focusable only if:
display: none
.visibility
isvisible
. This means that the nearest ancestor to havevisibility
set must have a value ofvisible
. If no ancestor hasvisibility
set, then the computed value isvisible
.More details are in another Stack Overflow answer.
3. Understand how tab order works
The tab order of elements in a document is controlled by the
tabindex
attribute. If no value is set, thetabindex
is effectively0
.The
tabindex
order for the document is: 1, 2, 3, …, 0.Initially, when the
body
element (or no element) has focus, the first element in the tab order is the lowest non-zerotabindex
. If multiple elements have the sametabindex
, you then go in document order until you reach the last element with thattabindex
. Then you move to the next lowesttabindex
and the process continues. Finally, finish with those elements with a zero (or empty)tabindex
.The core of the answer lies on finding the next element:
Usage:
Notice I don't care about prioritizing
tabIndex
.