I am using <a>
tags for links on a web page. How do I disable the Tab key from selecting either of them?
You can use :not() CSS selector with pointer-events: none; to disable click event. Show activity on this post. Simply add this class to the tabs that you want to disable the clicks.
The <a> tag doesn't have a disabled attribute, that's just for <input> s (and <select> s and <textarea> s). To "disable" a link, you can remove its href attribute, or add a click handler that returns false.
Alternatively you could go for plain HTML solution.
<a href="http://foo.bar" tabindex="-1">inaccessible by tab link</a>
The HTML5 spec says:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With