Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the tabindex="-1" in bootstrap for

What's the tabindex attribute in Bootstrap 3 for?

Its documentation doesn't say anything about them although they use them practically in all modals.

I've only find this regarding its use, which doesn't really say much

Accessible tooltips for keyboard and assistive technology users

For users navigating with a keyboard, and in particular users of assistive technologies, you should only add tooltips to keyboard-focusable elements such as links, form controls, or any arbitrary element with a tabindex="0" attribute.

And I found out I can't press esc to hide a modal if the tabindex attribute is not -1.

  • Modal closing after pressing esc key (with tabindex)
  • Modal not closing after pressing esc key (without tabindex)
like image 588
Alvaro Avatar asked Oct 02 '15 16:10

Alvaro


People also ask

What is the use of Tabindex 1?

tabindex="1" (or any number greater than 1) defines an explicit tab or keyboard navigation order. This must always be avoided. tabindex="0" allows elements besides links and form elements to receive keyboard focus.

What is the meaning of Tabindex ='- 1?

A negative value (usually tabindex="-1" ) means that the element is not reachable via sequential keyboard navigation, but could be focused with JavaScript or visually by clicking with the mouse. It's mostly useful to create accessible widgets with JavaScript.

What does Tabindex do in bootstrap?

The tabindex attribute specifies the tab order of an element (when the "tab" button is used for navigating).

What is the difference between Tabindex 0 and Tabindex =- 1?

tabindex= "0" allows elements besides links and form elements to receive keyboard focus. It does not change the tab order, but places the element in the logical navigation flow, as if it were a link on the page. tabindex= "-1" removes the element from the navigation sequence, but can be made focusable using javascript.


2 Answers

The tabindex attribute explicitly defines the navigation order for focusable elements (typically links and form controls) within a page. It can also be used to define whether elements should be focusable or not.

[Both] tabindex="0" and tabindex="-1" have special meaning and provide distinct functionality in HTML. A value of 0 indicates that the element should be placed in the default navigation order. This allows elements that are not natively focusable (such as <div>, <span>, and <p>) to receive keyboard focus. Of course one should generally use links and form controls for all interactive elements, but this does allow other elements to be focusable and trigger interaction.

A tabindex="-1" value removes the element from the default navigation flow (i.e., a user cannot tab to it), but it allows it to receive programmatic focus, meaning focus can be set to it from a link or with scripting.** This can be very useful for elements that should not be tabbed to, but that may need to have focus set to them.

A good example is a modal dialog window - when opened, focus should be set to the dialog so a screen reader will begin reading and the keyboard will begin navigating within the dialog. Because the dialog (probably just a <div> element) is not focusable by default, assigning it tabindex="-1" allows focus to be set to it with scripting when it is presented.

A value of -1 can also be useful in complex widgets and menus that utilize arrow keys or other shortcut keys to ensure that only one element within the widget is navigable with the tab key, but still allow focus to be set on other components within the widget.

Source: http://webaim.org/techniques/keyboard/tabindex

This is why you need tabindex="-1" on the modal <div>, so users can access common mouse and keyboard shortcuts. Hope that helps.

like image 69
Kyle Anderson Avatar answered Oct 13 '22 23:10

Kyle Anderson


The tabindex attribute is associated with HTML, it is not specific to Bootstrap.

This property is responsible for indicating if an element is reachable by keyboard navigation.

You need to look out for three different scenarios:

  1. When adding tabindex = "0" to an element this means it is reachable by the keyboard navigation but the order is defined by the documents source order.

  2. When adding a positive value to the tabindex attribute (for example tabindex = "1", tabindex = "2") those elements are reachable by keyboard navigation and the order is defined by the value of the attribute.

  3. When adding a negative value to tabindex (usually tabindex="-1") it means the element is not reachable by the keyboard navigation, but can be focused using focus function in JS.

like image 29
Nesha Zoric Avatar answered Oct 13 '22 23:10

Nesha Zoric