Hi.
I'd like to have the first input field on my forms auto-focused. How can I accomplish this in Drupal?

Have read some posts* on the matter, and it seems this feature is not going to be implemented, generally out of usability concerns, in the core module. However, all my forms are simple, no-guidance one-form-per-page sorts, and I feel this feature would really help my users, who are expected to comprise the spectrum of Internet users.

Cheers,
Vinay

*
Allow FAPI usage of the autofocus attribute, 11 Apr 2012
http://drupal.org/node/1174936

Usability: Autofocus textfields, 15 Sep 2011
http://drupal.org/node/90324

autoFocus and usability considerations, 2 Sep 2011
http://drupal.org/node/1179528

Focus on first input element (in popup), 8 Oct 2010
http://drupal.org/node/385732

Comments

Jaypan’s picture

Here is an extremely simple version.

1) Create THEME_DIRECTORY/focus.js

(function($, Drupal)
{
  Drupal.behaviors.autoFocus = {
    attach:function()
    {
      $(".form-text:first").focus();
    }
  };
}(jQuery, Drupal));

2) In your theme's .info file, add the following line of code:
scripts[] = focus.js

merrymuze’s picture

Jaypan,
Works almost like a charm (but you already knew that) ...

... so I combined your code with the example on this page (http://stackoverflow.com/questions/277544/how-to-set-the-focus-to-the-fi...) to extend its applicability for my forms.

Thank you / Arigatou gozaimasu!

Cheers,
Vinay

Jaypan’s picture

dou itashimashite :D

rroose’s picture

Nice! Thanks.