446

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.

6 Answers 6

724

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>

15
  • 15
    It appears Google Chrome does not support the -1, which makes sense since technically tabIndex only supports 0 -32767 according to linkW3. So when I did this; I used 500. Hackish; but worked.
    – Flea
    Commented Mar 23, 2012 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
    Commented Jan 14, 2013 at 14:32
  • 5
    I've edited the answer to link to the updated HTML5 specification. tabindex now allows to have negative values. Commented Apr 9, 2013 at 8:25
  • 1
    @JamesDonnelly Thank you for your edit. I re-added the reference to the W3C HTML4 spec for browser compability. Commented Apr 15, 2013 at 9:58
  • 2
    Supported since IE 5.01 msdn.microsoft.com/en-us/library/ie/ms534654(v=vs.85).aspx
    – Skurpi
    Commented Jun 11, 2014 at 11:24
164

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.

2
  • 44
    This seems like it should be a comment not an answer.
    – DrCord
    Commented Dec 14, 2015 at 21:23
  • 87
    Eh, I was glad to read it, and I probably would have missed it if it were buried as a comment. Commented Jun 8, 2016 at 23:58
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.

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.

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.

6
  • Which version of Chrome?
    – yaakov
    Commented Jun 30, 2016 at 1:16
  • Chrome 49.0.2623.75 (64 bit).
    – Felix Eve
    Commented Jun 30, 2016 at 1:27
  • My version is 51.0.2704.103. Check out this fiddle. You might have the wrong code, you never know.
    – yaakov
    Commented Jul 8, 2016 at 19:07
  • 1
    @AtulChaudhary, how doesn't it work? You can still focus it?
    – yaakov
    Commented Nov 16, 2016 at 17:56
  • once the fields are disabled they remain disabled and it is an issue in IOS9 but seems to be working in IOS10 Commented Nov 29, 2016 at 13:50
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.