[whatwg] Two propositions for the autofocus attribute

Jonas Sicking jonas at sicking.cc
Sun Apr 25 20:22:41 PDT 2010

On Sun, Apr 25, 2010 at 1:19 PM, Benjamin Hawkes-Lewis
<bhawkeslewis at googlemail.com> wrote:
> On Thu, Apr 15, 2010 at 11:43 PM, Mounir Lamouri
> <mounir.lamouri at gmail.com> wrote:
>> 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.
> But it wouldn't prevent /focus/ moving, since publishers could call
> focus(), so from a user perspective I can't see the advantage?

The purpose isn't to try to protect the user from web site that
behaves badly. The purpose is to give webdevelopers tools that makes
it easy to develop user friendly pages.

>> - 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().
> I think focusing using autofocus is better than forcing developers
> to use focus().
> Consider a dialog box added to a webpage using JS. "autofocus"
> allows separation of concerns between the layout of controls within
> the dialog and the script. If you use focus() only, you must update
> the script with knowledge of which control to focus as the layout of
> controls changes. If you use "autofocus", the first focused control
> in the dialog can be changed without touching the script.

That generally won't work as focus is generally in another part of the
page, and so the UA won't focus the new control.

I think calling .focus() is the best option here if you really want to
manually move focus.

/ Jonas

More information about the whatwg mailing list