Which HTML elements can receive focus?
There isn't a definite list, it's up to the browser. The only standard we have is DOM Level 2 HTML, according to which the only elements that have a focus()
method areHTMLInputElement
, HTMLSelectElement
, HTMLTextAreaElement
and HTMLAnchorElement
. This notably omits HTMLButtonElement
and HTMLAreaElement
.
Today's browsers define focus()
on HTMLElement, but an element won't actually take focus unless it's one of:
- HTMLAnchorElement/HTMLAreaElement with an href
- HTMLInputElement/HTMLSelectElement/HTMLTextAreaElement/HTMLButtonElement but not with
disabled
(IE actually gives you an error if you try), and file uploads have unusual behaviour for security reasons - HTMLIFrameElement (though focusing it doesn't do anything useful). Other embedding elements also, maybe, I haven't tested them all.
- Any element with a
tabindex
There are likely to be other subtle exceptions and additions to this behaviour depending on browser.
Can multiple HTML elements receive focus at the same time?
No, you can only focus on one element at a time.
Make div element receive focus
Set the tabindex="0"
on the div
, on the assumption that you want the div
s to receive focus, rather than child elements.
Updated the answer to add a JS Fiddle demo, showing a JavaScript means of achieving this:
var divs = document.getElementsByTagName('div');
for (var i = 0, len = divs.length; i < len; i++){
divs[i].setAttribute('tabindex', '0');
}
JS Fiddle demo.
While composing the demo I found that tabindex="-1"
allows the element to receive mouse-clicks to assign focus, but not keyboard events (through the tab button), however (and as implied in the comment, earlier, by Fabricio Matté, a tabindex="0"
allows for both). So I've used that in the demo, and updated the original answer to reflect that change.
Behavior of tabindex="0"
versus tabindex="-1"
documented here: https://developer.mozilla.org/en-US/docs/Web/Accessibility/Keyboard-navigable_JavaScript_widgets#Using_tabindex
Make an HTML element non-focusable
<a href="http://foo.bar" tabindex="-1">unfocusable</a>
A negative value means that the element should be focusable, but should not be reachable via sequential keyboard navigation.
See also: developer.mozilla.org
How to check if a dom element is focusable?
Answer "translated" from here: Which HTML elements can receive focus?
<a>
or<area>
withhref
- Any form elements which aren't disabled
- iframes
- Any element with
tabindex
Additionaly, I believe that hidden elements can't get focus also.
Assuming that conditions, the following function may help you (assuming it'll always receive an jQuery element):
function canFocus( $el ) {
if ( $el.is( ":hidden" ) || $el.is( ":disabled" ) ) {
return false;
}
var tabIndex = +$el.attr( "tabindex" );
tabIndex = isNaN( tabIndex ) ? -1 : tabIndex;
return $el.is( ":input, a[href], area[href], iframe" ) || tabIndex > -1;
}
How to disable focus on specific html elements?
The tabindex attribute controls tabbing. Set it to -1 and the tab key will not stop on that element.
<button tabindex="-1">click me</button>
Related Topics
How to Nest a ≪Button≫ Element Inside an ≪A≫ Using Html5
How to Keep Two Side-By-Side Div Elements the Same Height
Disabled Form Inputs Do Not Appear in the Request
How to Position Text Over an Image With Css
Why Doesn't the Height of a Container Element Increase If It Contains Floated Elements
Vertically Align an Image Inside a Div With Responsive Height
Mobile Redirect Using Htaccess
How to Remove Margin Space Around Body or Clear Default CSS Styles
Canvas Width and Height in Html5
How to Style the Parent Element When Hovering a Child Element
Floated Elements of Variable Height Push Siblings Down
How to Get the File Path from HTML Input Form in Firefox 3
Element Will Not Stay Centered, Especially When Re-Sizing Screen
How to Disable a Checkbox Based on Conditions in Angular 6
On Hover of Child, Change Background Color of Parent Container (Css Only)
Limit File Format When Using ≪Input Type="File"≫
Can't Scroll to Top of Flex Item That Is Overflowing Container