Set focus on first field with jQuery

f you are using Prototype see this post.

Setting focus on the first text field with jQuery is as simple as


  $("input:text:visible:first").focus();

Find more at http://docs.jquery.com/Selectors

16 thoughts on “Set focus on first field with jQuery

  1. If you want to have more control about this there’s another alternative with this plugin:

    http://github.com/dcadenas/auto_focusable_forms/tree/master

    -After you install it you will have automatically focused your first inputs without adding a single line of code to your app.

    -Another nice thing is that you can disable this behaviour by just passing “:autofocus => false” to your forms_for.

    -Another advantage is that there’s no dependency with Prototype or JQuery.

    Oh sorry, there are some dependencies, you’ll need Rails and a browser that can understand javascript😉

    Like

  2. how about $(”input:text:visible:first”).focus();?
    I am not sure how filter work, but if they are taken from left to right, then visible should be filtered out first.

    Like

  3. “how about $(”input:text:visible:first”).focus();?”

    Actually, that should be: $(“:input:text:visible:first”).focus();

    This will grab focus to the first form field, regardles sof type (select, input, textarea).

    Like

  4. How would you do this to a named input field? Or should I assign it a class or id value?

    I would rather do it by its name.

    Thanks.
    Bill

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s