views:

3295

answers:

5

Hi,

I am using tags for links on a web page. How do I disable tab key from selecting either of them.

A: 

Try

<a onfocus="this.blur();" href = "bla">Bla</a>
nlaq
Beat me by 39 seconds, Nelson. ;-)
Evan Fosmark
Thanks, Worked like a charm :)
Rakesh
Not nice for accessibility/laptop keyboard users/etc. As the focus cycles into the element it gets completely lost, making it impossible to tab onto the next link. Better to take it out of the regular tab cycle using a tabindex attribute.
bobince
A: 

You could do something like this for those links:

 <a href="http://foo.bar" onfocus="this.blur()">Can't focus on this!</a>

You should use the answer below, though.

Evan Fosmark
Thanks, Worked like a charm :)
Rakesh
This is not the right way to do it. tabIndex property is.
Alex
Alex, that's why I updated my answer to say that it's better to use the one below.
Evan Fosmark
+29  A: 

Alternatively you could go for plain HTML solution.

<a href="http://foo.bar" tabIndex="-1">inaccessible by tab link</a>

Sergey Ilinsky
and works without script
annakata
@annakata, I think that was Sergey's point. Heh.
Evan Fosmark
+1 Always favour non-script solutions (that work!) over non-script solutions.
cletus
Oddly enough people have not yet suggested a nowdays panacea solution - jQuery code sample
Sergey Ilinsky
Does it validate against W3C HTML validator ?
Guido
@Evan - yeah I guess implicitly :)
annakata
It does not validate. http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex says "This value must be a number between 0 and 32767." Is there an easy valid way to do this?
dfrankow
+1  A: 

I've had to prevent divs with and overflow: auto css rule from having a tab stop before and what I did was (transposed for a's):

var links = document.getElementsByTagName( 'a' );

for( var i = 0, j =  links.length; i < j; i++ ) {
    links[i].setAttribute( 'tabindex', '-1' );
}

Using tabindex rather than blurring means the focus will skip to the next element.

Are you sure you want to disable tabindex though? It's kinda vital for navigation without a mouse.

Just noticed a similar answer in plain HTML

meouw
A: 

I like meouw's version because it validates and it is easy to implement across all anchor tags. However, it doesn't seem to work in IE. Putting tabIndex="-1" in every single anchor tag appears to be the only solution IE will recognize. Someone please correct me because I would love to use this script.

poster493029482
Consider asking a question of your own instead of posting in someone else's. Note the big "Ask Question" button in the top-right corner of your screen.
Bart Kiers