Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would an `a` tag need `tabindex=0`?

Tags:

I'm working on a web app, and one of the repeated a (anchor) elements does not get keyboard focus as I tab through the page. Only if I add tabindex=0 can I tab to it.

(Although my goal is to make the focus visible, I'm determining whether an element gets focus by using a snippet of jQuery:

// Whenever I hit tab, show me which element has focus
$('main').on('keydown',function(e) { 
  var keyCode = e.keyCode || e.which; 

  if (keyCode == 9) { 
    console.log("focus is now on ", $(':focus'));
  } 
});

)

This confuses me. According to the specs, "The tabindex attribute can also make any element into interactive content" - but a is one of the ones they list as interactive by default.

And again, from an accessibility article:

A [tabindex] 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... (http://webaim.org/techniques/keyboard/tabindex)

What would cause an anchor element to be skipped as I tab through the interactive elements of a page?

like image 759
Nathan Long Avatar asked Jun 09 '15 18:06

Nathan Long


People also ask

What is the use of Tabindex 0?

tabindex="0" means that the element should be focusable in sequential keyboard navigation, after any positive tabindex values and its order is defined by the document's source order.

What will happen if you set the Tabindex of a control to zero 0 )?

A tabindex value of zero A tabindex equals to exactly zero will place the element in the default focus order, determined by its position in the source HTML. It can be applied to elements that are not typically focusable, and will add them to the natural focus order as if they were.

What is Tabindex in a tag?

Definition and Usage The tabindex attribute specifies the tab order of an element (when the "tab" button is used for navigating). The tabindex attribute can be used on any HTML element (it will validate on any HTML element.

When should Tabindex be used?

The tabindex attribute of 1+ 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.


1 Answers

As per your question:

What would cause an anchor element to be skipped as I tab through the interactive elements of a page?

If you add tabindex="-1" the element will be skipped.
And if your <a> tag has no href, it will be skipped as well.

like image 196
taxicala Avatar answered Sep 20 '22 02:09

taxicala