Use tabindex to Specify the Order of Keyboard Focus for Several Elements
The tabindex
attribute also specifies the exact tab order of elements. This is achieved when the attribute's value is set to a positive number of 1 or higher.
Setting a tabindex="1"
will bring keyboard focus to that element first. Then it cycles through the sequence of specified tabindex
values (2, 3, etc.), before moving to default and tabindex="0"
items.
It's important to note that when the tab order is set this way, it overrides the default order (which uses the HTML source). This may confuse users who are expecting to start navigation from the top of the page. This technique may be necessary in some circumstances, but in terms of accessibility, take care before applying it.
Here's an example:
<div tabindex="1">I get keyboard focus, and I get it first!</div>
<div tabindex="2">I get keyboard focus, and I get it second!</div>
Camper Cat has a search field on his Inspirational Quotes page that he plans to position in the upper right corner with CSS. He wants the search input
and submit input
form controls to be the first two items in the tab order. Add a tabindex
attribute set to 1
to the search
input
, and a tabindex
attribute set to 2
to the submit
input
.
Another thing to note is that some browsers may place you in the middle of your tab order when an element is clicked. An element has been added to the page that ensures you will always start at the beginning of your tab order.
Tests
- Waiting: 1. Your code should add a
tabindex
attribute to thesearch
input
tag. - Waiting: 2. Your code should add a
tabindex
attribute to thesubmit
input
tag. - Waiting: 3. Your code should set the
tabindex
attribute on thesearch
input
tag to a value of 1. - Waiting: 4. Your code should set the
tabindex
attribute on thesubmit
input
tag to a value of 2.
/** * Your test output will go here */