Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML where will the focus go next if I press tab?

Is there some way of knowing where will the focus jump to when the tab key key will be pressed and certain element has the focus?

I am thinking on something to be used this way:

var nextElement = whereWillFocusJumpTo(currentElement);

Thanks!

like image 246
DanC Avatar asked Jun 17 '10 20:06

DanC


People also ask

How does focus work in HTML?

The HTMLElement. focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will receive keyboard and similar events by default.

How do I move focus in HTML?

In most browsers, users can move focus by pressing the Tab key and the Shift + Tab keys. The following elements can receive focus: <a> tags with an href attribute.

How do you go to next tab in HTML?

In summaryThe href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.

Does focus scroll to Element?

An element can be focused by either using the autofocus="true" attribute or calling the element. focus() method. In both cases, the browser will automatically scroll the element into the viewport.


3 Answers

Use HTML's TABINDEX attribute to control where the tab goes.

<input name="email" tabindex="1"></input>
<input name="phone" tabindex="2"></input>
like image 103
Damien Dennehy Avatar answered Oct 27 '22 22:10

Damien Dennehy


The algorithm for determining the tab order is here:-

   http://dev.w3.org/html5/spec/editing.html#sequential-focus-navigation

One thing to note is that if more than one element has a tabindex of 0, the tab order is platform dependent, so you may wish to ensure that all focusable elements on your page have a non-zero tabindex.

like image 35
Alohci Avatar answered Oct 27 '22 23:10

Alohci


Maybe you can use the DOM to enumerate the inputs on the page and read the tabindex property.

like image 27
Steve Sheldon Avatar answered Oct 27 '22 21:10

Steve Sheldon