Tuesday, April 5, 2011

How do I disable tabs for <a> tag

Hi,

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

From stackoverflow
  • Try

    <a onfocus="this.blur();" href = "bla">Bla</a>
    
    Evan Fosmark : Beat me by 39 seconds, Nelson. ;-)
    Rakesh : Thanks, Worked like a charm :)
    bobince : 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.
  • 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.

    Rakesh : Thanks, Worked like a charm :)
    Alex : This is not the right way to do it. tabIndex property is.
    Evan Fosmark : Alex, that's why I updated my answer to say that it's better to use the one below.
  • Alternatively you could go for plain HTML solution.

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

    annakata : and works without script
    Evan Fosmark : @annakata, I think that was Sergey's point. Heh.
    cletus : +1 Always favour non-script solutions (that work!) over non-script solutions.
    Sergey Ilinsky : Oddly enough people have not yet suggested a nowdays panacea solution - jQuery code sample
    Guido : Does it validate against W3C HTML validator ?
    annakata : @Evan - yeah I guess implicitly :)
    dfrankow : 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?
  • 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

  • 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.

    Bart Kiers : 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.

0 comments:

Post a Comment