Prevent tabstop on A element (anchor link) in HTML
Asked Answered
K

5

167

Is it possible to cancel an <a href="..."> from being tabstopped in any browser? I would like to do this without Javascript.

Kipkipling answered 13/10, 2009 at 15:34 Comment(0)
P
292

Some browsers support the tabindex="-1" attribute, but not all of them, since this is not a standard behaviour.

Profusion answered 13/10, 2009 at 15:43 Comment(3)
Oh great! Thanks! I've checked with FF3.5, IE8 and CH3 and it work in all three. Thanks a million!Kipkipling
No worries about this: just like zzzzBov states here, HTML5 came to the rescue and standardized this functionality. So now the guilty browsers are those that miss this.Cellaret
Keep in mind that disabling the tabstop on a hyperlink goes against accessibility rules (e.g. people using screen readers, etc). If you know that this isn't a problem for your user base, then it should be fine.Confiteor
H
80

Modern, HTML5 compliant, browsers support the [tabindex] attribute, where a value of -1 will prevent the element from being tabbed to.

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

Halutz answered 26/8, 2012 at 6:30 Comment(1)
Documentation excerpts always help. ;-) This tells us that those not supporting this are among the few and they'll likely support it sooner or later.Kipkipling
J
17

You could apply a JQuery handler to the element you want to target multiple elements with no tab stop.

$(document).ready(function () {
    $('.class').attr('tabindex', '-1');
});

Would be one way to do it....

Jerad answered 2/10, 2012 at 20:1 Comment(0)
M
6

I think you could do this by javascript, you override the window.onkeypress or onkeydown, trap the tab button, and set the focus at the desired order.

Manyplies answered 30/9, 2010 at 15:4 Comment(3)
I appreciate the alternative solution even if it doesn't address the OP as well as the accepted answer. No need to be disparaging.Appleby
@Anthony DiSanti: That is true, but in this particular case I still dn't see why would one resort to javascript if something else works better? So if it's not an answer to this question why is it here then? Nevermind. Javascript is usually the last step one would take if certain things can't be done otherwise. And I'm sorry @Manyplies if I've insulted you. I didn't mean to be rude. Thanks for the late answer.Kipkipling
I agree with avoiding javascript if there is a standards-based HTML or CSS solution. However, in this case there is not. The original poster didn't need to support browsers prior to IE8 and FF3.5, but for my work I need to support back to IE6. The tabindex solution is therefore not applicable. Providing the only working solution in the browser with the greatest market share should not be discouraged.Appleby
S
3

Remove the href attribute from your anchor tag

Spiritualize answered 4/6, 2014 at 16:30 Comment(3)
what if I can not?Miracle
If willing to use javascript despite the question to avoid that kind of solutions, to remove href attributes, use jquery document onload with something like $('[href="whatever-the-url-is"]').removeAttr('href');Egocentrism
LOL remove the hrefVernalize

© 2022 - 2024 — McMap. All rights reserved.