I'm looking for a definitive list of HTML elements which are allowed to take focus, i.e. which elements will be put into focus when focus()
is called on them?
I'm writing a jQuery extension which works on elements that can be brought into focus. I hope the answer to this question will allow me to be specific about the elements I target.
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 are
HTMLInputElement
, 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:
disabled
(IE actually gives you an error if you try), and file uploads have unusual behaviour for security reasonstabindex
There are likely to be other subtle exceptions and additions to this behaviour depending on browser.