Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tabindex: A Quick Look

DZone's Guide to

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.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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.

Values

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="1".

Usage

Tabindex can be used on the following elements:

  • input
  • anchor (a)
  • textarea
  • select
  • button

After creating any of these elements, give the text a proper line-height.

Example

HTML

<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.

If you found this article interesting and useful, check out other subjects such as relative font size, CSS columns and a little something about SASS and LESS.

Summary

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!

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
web dev ,html ,tabindex ,front-end development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}