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?
views:
521answers:
2
+1
A:
Button, Checkbox, FileUpload, Layer, Password, Radio, Reset, Select, Submit, Text, Textarea, window
Faruz
2009-10-21 09:29:30
Most of these aren't discrete HTML elements, just types of input element. Not trying to be picky, but the HTML elements are what I'm after.
Programming Hero
2009-10-21 09:46:55
+3
A:
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:
- 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.
bobince
2009-10-21 11:19:15