Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting tabindex to "last element" and "penultimate element" - possible?

In the accepted answer to this question Multiple submit buttons in an HTML form the comment is raised:

Please don't do this without also changing the tab order, so that hitting the tab button will cycle through the buttons as they appear on screen.

My question is: is there a way to set the tabindex on those two buttons to accomplish this ordering without having to assign a specific tabindex to every other tabable element on the page?

My understanding of tabindex values is that specified positive values proceed elements w/o a specified value, so I am at a loss to figure out a way to do this w/o going through and assigning everything else a value.

If indeed, assigning a specific tabindex to every item is the only way, is there some (hopefully short and hopefully jQuery) magic to assign every appropriate element on the page a tabindex of, say, 1?

EDIT
As it looks like the solution is going to involve applying a specific tabindex to every other tabable object -- it seems like an important part of the solution is going to be: is there a convenient way in jQuery to select every tabable object? All s s s and ???

like image 338
John Hascall Avatar asked Jan 05 '16 15:01

John Hascall


People also ask

Can multiple elements have same Tabindex?

If multiple elements share the same positive tabindex value, their order relative to each other follows their position in the document source. The maximum value for tabindex is 32767. If not specified, it takes the default value 0.

How do I change my Tabindex order?

Another related tactic is to use the tabindex attribute. A tabindex of -1 (tabindex="-1") will remove an element from the tab order. A tabindex of 0 (tabindex="0") will add an element to the focus order. It is highly recommended to avoid positive tabindex values and to structure your DOM in the correct order.

How do you prevent Tabindex?

The way to do this is by adding tabindex="-1" . By adding this to a specific element, it becomes unreachable by the keyboard navigation.

Can we add Tabindex in CSS?

Yes. It is useful. The most useful values are tabindex="0" for example on <span/> or <div/> element and tabindex="-1" to disable tab stops or make elements focusable without tab-navigation. I created HTML+CSS libraries that use this.


1 Answers

According to the specification:

  • positiv values assigned to tabindex orders the elements according to their tabindex values

  • negative values make elements "unfocusable"

  • a value of 0 makes the element focusable but its order dependents on the platform

mdn-html specification of tabindex

So if you want to have a specific order in your page you have to assign a value to each element.

But here comes jquery: Say the elements which should be in order are in a div with id="myDiv" You can then do:

$("#myDiv").find("*").prop("tabindex", 1);

This would make every child/subchild element of myDiv have a tabindex of 1. Then your two buttons could have a css class assigned (e.g: class="highTabIndex").

Then you can call jquery again:

var idx = 2;
$("#myDiv").find(".highTabIndex").each(function(idx, element) {
    element.prop("tabindex", idx++);
});

and your buttons with class highTabIndexwould be orderd according to "position" in the page.

like image 59
Flocke Avatar answered Oct 17 '22 21:10

Flocke