Tabindex: A Quick Look
Tabindex: A Quick Look
In this short article, we take a look at how to include tabindex in your front-end code. By mastering this, you will write more efficient and user-friendly forms.
Join the DZone community and get the full member experience.Join For Free
Learn how to add document editing and viewing to your web app on .Net (C#), Node.JS, Java, PHP, Ruby, etc.
When dealing with element focusing, tabindex plays a big role. This attribute indicates if the element can be focusable and reachable via keyboard navigation. When pressing the Tab key, the browser shifts focus from one element to another. By using tabindex, we can change the tab order flow.
When creating an element, styling should be included.
Tabindex accepts integer values. They can be:
- Negative (usually
tabindex="-1") - the element is not reachable through keyboard navigation.
tabindex="0"- the element is reachable by the keyboard navigation but the order is defined by the document's source order.
- Positive - the element is reachable by the keyboard navigation, and the order is defined by the value. For example,
tabindex="2"is focusable before
tabindex="3", but after
Tabindex can be used on the following elements:
- anchor (a)
After creating any of these elements, give the text a proper line-height.
<body> <label>This element will be focused second</label> <input name=”field1” type=”email” tabindex=”2” /> <label>This element will be focused first</label> <textarea rows=”4” cols=”5” tabindex=”1”></textarea> <label>The second focused last</label> <input name=”field3” type=”text” tabindex=”3”/> </body>
Here we can see the order of the tab selection, starting from the lowest to the highest number. If an element has
tabindex="0", it will be excluded from the tab order. If two elements have the same tabindex value, they will be selected in the order which they appear in the element.
This short article summarizes the usage of the tabindex. By mastering this, you will write more efficient and user-friendly forms.
Thank you for reading and happy coding!
Published at DZone with permission of Tatjana Boskovic , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.