437

Is there any way in HTML to tell the browser not to allow tab indexing on particular elements?

On my page though there is a sideshow which is rendered with jQuery, when you tab through that, you get a lot of tab presses before the tab control moves to the next visible link on the page as all the things being tabbed through are hidden to the user visually.

Piper
  • 1,266
  • 3
  • 15
  • 26
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

6 Answers6

708

You can use tabindex="-1".

Only do this if you are certain it does not remove functionality for keyboard users.

The W3C HTML5 specification supports negative tabindex values:

If the value is a negative integer
The user agent must set the element's tabindex focus flag, but should not allow the element to be reached using sequential focus navigation.


Watch out though that this is a HTML5 feature and might not work with old browsers.
To be W3C HTML 4.01 standard (from 1999) compliant, tabindex would need to be positive.


Sample usage below in pure HTML.

<a href="#" onclick="return false">Focusable</a>
<a tabindex="-1" href="#" onclick="return false">Not focusable</a>
<a href="#" onclick="return false">Focusable</a>
Andy
  • 4,783
  • 2
  • 26
  • 51
Martin Hennings
  • 16,418
  • 9
  • 48
  • 68
  • 15
    It appears Google Chrome does not support the -1, which makes sense since technically tabIndex only supports 0 -32767 according to [link](http://www.w3.org/TR/html401/interact/forms.html#h-17.11.1)`W3`. So when I did this; I used 500. Hackish; but worked. – Flea Mar 23 '12 at 20:34
  • 45
    @Flea As of version 23, Google Chrome supports -1 on tabindex. Not sure how long ago that happened, perhaps prior to 23. I tested "-1" in Chrome 23, Firefox 18, IE8, IE9, and Opera 12.11 and it worked across the board. – jkupczak Jan 14 '13 at 14:32
  • 5
    I've edited the answer to link to the updated HTML5 specification. `tabindex` now allows to have negative values. – James Donnelly Apr 09 '13 at 08:25
  • 1
    @JamesDonnelly Thank you for your edit. I re-added the reference to the W3C HTML4 spec for browser compability. – Martin Hennings Apr 15 '13 at 09:58
  • 2
    Supported since IE 5.01 http://msdn.microsoft.com/en-us/library/ie/ms534654(v=vs.85).aspx – Skurpi Jun 11 '14 at 11:24
  • @jimmykup Chrome 38, tabindex=-1 participates in tab stop round robin. – pronebird Oct 26 '14 at 15:49
  • Also start off your tab index with 1, not 0 like some references say is OK. It's not. –  Nov 12 '14 at 14:36
  • @MikeBethany Interesting - do you have a source? – Martin Hennings Nov 13 '14 at 08:52
  • @Martin Yes, here are examples. Tabindex 0: https://gist.github.com/mikbe/e4b68c89f678556afd8e -- Tabindex 1: https://gist.github.com/mikbe/6cb1254a3cca53a2b8d2 -- Notice the tabindex 0 version jumps from the tabindex 0 to the address bar while tabindex 1 version properly goes from 1 to 2. –  Nov 13 '14 at 18:05
  • @MikeBethany I meant a source, a reference, an official statement, a w3 link or similar, but your claim is empirical and maybe not applicable for all browsers. – Martin Hennings Nov 14 '14 at 12:10
  • 1
    @Martin "...and maybe not applicable for all browsers." And that's my point. Your tabindex should work with all browsers so if one of the major browsers has an issue you have to code for that. I also only care about empirical facts, I don't care about hypotheticals. –  Nov 14 '14 at 15:17
  • See my answer which works for almost all modern chrome, and for most browsers. http://stackoverflow.com/a/38005077/681830 – Val Dec 20 '16 at 10:36
  • Note for GWT developers. Since GWT has special handling for -1 value, set any offer negative value (e.g. `tabIndex="-2"`) – foal Jan 24 '17 at 07:40
  • Ok, and what is GWT ? – Kalnode Aug 01 '22 at 22:44
155

Don't forget that, even though tabindex is all lowercase in the specs and in the HTML, in Javascript/the DOM that property is called tabIndex.

Don't lose your mind trying to figure out why your programmatically altered tab indices calling element.tabindex = -1 isn't working. Use element.tabIndex = -1.

Eric L.
  • 3,232
  • 2
  • 22
  • 20
15

If these are elements naturally in the tab order like buttons and anchors, removing them from the tab order with tabindex="-1" is kind of an accessibility smell. If they're providing duplicate functionality removing them from the tab order is ok, and consider adding aria-hidden="true" to these elements so assistive technologies will ignore them.

Bulmenisaurus
  • 47
  • 3
  • 7
Matt
  • 455
  • 3
  • 13
10

If you are working in a browser that doesn't support tabindex="-1", you may be able to get away with just giving the things that need to be skipped a really high tab index. For example tabindex="500" basically moves the object's tab order to the end of the page.

I did this for a long data entry form with a button thrown in the middle of it. It's not a button people click very often so I didn't want them to accidentally tab to it and press enter. disabled wouldn't work because it's a button.

JemWritesCode
  • 502
  • 6
  • 17
5

Just add the attribute disabled to the element (or use jQuery to do it for you). Disabled prevents the input from being focused or selected at all.

yaakov
  • 4,568
  • 4
  • 27
  • 51
5

The way to do this is by adding tabindex="-1". By adding this to a specific element, it becomes unreachable by the keyboard navigation. There is a great article here that will help you further understand tabindex.

Nesha Zoric
  • 6,218
  • 42
  • 34