[whatwg] Two propositions for the autofocus attribute

Garrett Smith dhtmlkitchen at gmail.com
Sun Apr 25 16:26:51 PDT 2010


On Thu, Apr 15, 2010 at 3:43 PM, Mounir Lamouri
<mounir.lamouri at gmail.com> wrote:
> Hi,
>
> At the moment, the autofocus attribute specification [1] is quite
> permissive: only one element should have the autofocus enabled in the
> document but each time an element with autofocus is inserted into the
> document, the UA should give it the focus. The UA can disable the
> autofocus request for some reasons like if the user is already typing
> into a document.
>

The specification uses the term "user" in double meaning, in the same sentence:

| The autofocus content attribute allows the user to
| indicate that a control is to be focused as soon as the
| page is loaded, allowing the user to just start typing
| without having to manually focus the main control.

Change to:
"The autofocus content attribute allows the author..."

> As far as I know, the autofocus attribute has been introduced to
> autofocus a form field during the load process to prevent doing that in
> js and thus focusing an element after the load of the document.
>
> To better fulfill this need, I think we should add two rules for the
> autofocus attribute behavior:
> - only the first element with the autofocus attribute specified should
> get the focus. All other autofocus requests should be ignored. This is
> exactly the same for authors because they should not have more than one
> element with the autofocus attribute specified but that would be better
> for the user because it will prevent autofocus to move from a field to
> another.

Right; having focus shift from one control to another would be bad.

The author adding a second autofocus'd element to the page might
observe that the element does not get autofocus. It is then his task
to correct his mistake and remove autofocus from the first.

> - if an element with the autofocus attribute specified is inserted after
> the load event (or the 'DOMContentLoaded' event) it should not get the
> focus. Having an element inserted in the document with the autofocus
> attribute specified is just a way to prevent using .focus(). That is not
> why autofocus has been introduced. If an author want to focus an element
> after the load event, he/she should use .focus().
>

Using focus() is easy enough to do.

> What is your opinion about these two propositions ?
>

The autofocus encourages inherently bad usability. See my response to
Jonas as well as timeless' example.

> [1] http://dev.w3.org/html5/spec/forms.html#attr-fe-autofocus
>

That page is heavy and throws javascript errors.

Firefox 3.6:
"uncaught exception: Failed to find TOC"

IE7 has three errors:
1) "Exception thrown and not caught."
2) "Object doesn't support this property or method."
3) "Object doesn't support this property or method."

Ouch.

Garrett


More information about the whatwg mailing list