See which controls Tab reaches in static HTML, without running scripts.
dotnet add package RecursiveAI.OneHtml.AccessibilityRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneHtml; using RecursiveAI.OneHtml.Accessibility; using RecursiveAI.OneHtml.Css; var doc = Document.Parse(""" <button>Save</button> <button tabindex="1">Help</button> <button tabindex="-1">Back</button> <button disabled>Next</button> <button hidden>Done</button> """); var tree = AccessibilityTree.Build(doc, DocumentStyles.Create(doc)); foreach (var stop in tree.FocusOrder) Console.WriteLine(stop.Name.Value);
Unzip the example, then dotnet run.
Prints the names of the controls in tab order.
Help Save
The full program
The download shows the full sequence and why individual controls are in or out. A node with SequentialIndex == -1 isn’t a Tab stop; if an element has no accessibility node, Find returns null instead.
using RecursiveAI.OneHtml; using RecursiveAI.OneHtml.Accessibility; using RecursiveAI.OneHtml.Css; var doc = Document.Parse(""" <button id="save">Save</button> <button id="continue" tabindex="2">Continue</button> <button id="help" tabindex="1">Help</button> <button id="cancel" tabindex="1">Cancel</button> <button id="back" tabindex="-1">Back</button> <button id="next" disabled>Next</button> <button id="done" hidden>Done</button> <a id="details" href="/details" tabindex="0">Details</a> """); var styles = DocumentStyles.Create(doc); var tree = AccessibilityTree.Build(doc, styles); Console.WriteLine("Tab: " + string.Join(" -> ", tree.FocusOrder.Select(n => n.Name.Value))); foreach (var id in new[] { "save", "help", "back", "next", "done", "details" }) { var element = doc.Root.QuerySelector("#" + id); var node = tree.Find(element); if (node is null) { Console.WriteLine($"{id}: no accessibility node"); continue; } var focus = node.Focus; Console.WriteLine($"{id}: focusable={focus.IsFocusable}, " + $"tabindex={focus.TabIndex?.ToString() ?? "unset"}, index={focus.SequentialIndex}"); }
dotnet run prints:
Tab: Help -> Cancel -> Continue -> Save -> Details save: focusable=True, tabindex=unset, index=3 help: focusable=True, tabindex=1, index=0 back: focusable=True, tabindex=-1, index=-1 next: focusable=False, tabindex=unset, index=-1 done: no accessibility node details: focusable=True, tabindex=0, index=4
Variations
Skip an inert subtree
Replace the markup in the snippet at the top. inert keeps the Cancel button out of the tab sequence even though it isn’t marked disabled.
// Replace the short snippet's HTML with: var doc = Document.Parse(""" <button>Save</button> <div inert><button>Cancel</button></div> <a href="/help">Help</a> """);
Save Help
Related recipes
The short answer
Build the accessibility tree for your HTML, then read FocusOrder to get the sequential tab stops. To find out why a specific control isn’t listed, look it up with Find and read Focus.IsFocusable and Focus.SequentialIndex if it has a node. The two fields answer different questions: can this element take focus, and will Tab reach it?
What is tab order in accessibility?
Tab order is the order in which keyboard focus moves through interactive elements. It isn’t screen-reader reading order: a screen reader can navigate headings, paragraphs and other content that isn’t a Tab stop. For example, a heading between two buttons needn’t be focusable for a screen reader to find it. The WCAG explanation of focus order distinguishes sequential focus from the order used to understand page content.
The download uses names to make each stop easy to spot, but names aren’t unique identifiers. If two buttons have the same name, use the DomNode on the result to tell them apart. The example inspects one state of a document; it doesn’t tell you which element a script focuses after a dialog opens or what happens when the dialog closes.
What’s the difference between tabindex="0" and tabindex="-1"?
A value of 0 puts a focusable element into the ordinary sequence, after positive values. A value of -1 keeps an element out of the sequence while still allowing programmatic focus if it’s otherwise focusable. That’s why the download shows back as focusable=True with index=-1, and details as a Tab stop with tabindex=0. See the HTML tabindex reference.
next also has index=-1, but it is not focusable because it’s disabled. And the hidden button has no accessibility node at all in this example. Listing only the tab stops can’t distinguish those cases. Read the per-element focus record to tell what was skipped and whether it can still take focus.
How do I change the tab order in HTML?
In onehtml’s focus model, positive values are sorted numerically, with document order breaking ties. In the download, the two 1 buttons come before Continue (2), even though it’s earlier in the source. After that come the ordinary stops, including the explicit 0, in document order.
For a page you control, change the source order to match the intended task rather than numbering the controls with positive values. For example, put the address fields before the payment button if that’s the order the user needs them. Positive values can move a control ahead of unrelated controls elsewhere in the page. Keep native buttons and links in the normal order where possible, and use the report to spot unexpected jumps before checking the running page with a keyboard.