<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wolfmans Howlings: Setting the focus in a form</title>
    <link>http://blog.wolfman.com/articles/2006/10/29/setting-the-focus-in-a-form</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A programmers Blog about Ruby, Rails and a few other issues</description>
    <item>
      <title>Setting the focus in a form</title>
      <description>&lt;p&gt;This is a simple one, how do I set the focus in the first item in my form?&lt;/p&gt;

&lt;p&gt;Put this in your &lt;code&gt;app/helpers/application_helper.rb&lt;/code&gt;:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;  &lt;span class="comment"&gt;# put this in the body after a form to set the input focus to a specific control id&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;set_focus_to_id&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="punct"&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class="constant"&gt;END&lt;/span&gt;&lt;span class="string"&gt;
    &amp;lt;script language=&amp;quot;javascript&amp;quot;&amp;gt;
        &amp;lt;!--
                document.getElementById(&amp;quot;&lt;span class="expr"&gt;#{id}&lt;/span&gt;&amp;quot;).focus()
        //--&amp;gt;
    &amp;lt;/script&amp;gt;
&lt;/span&gt;&lt;span class="constant"&gt;    END&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then in your .rhtml file somewhere after the form is defined add this&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%= set_focus_to_id 'user_login' %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where user_login will be the id of the field you want to get the focus.&lt;/p&gt;

&lt;p&gt;An example of a login form...&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;p&amp;gt;
Please Login
&amp;lt;/p&amp;gt;
&amp;lt;% form_for :user do |f| %&amp;gt;
  &amp;lt;label for="user_login"&amp;gt;Login&amp;lt;/label&amp;gt;
  &amp;lt;%= f.text_field :login, :tabindex =&amp;gt; "1" %&amp;gt;
  &amp;lt;br /&amp;gt;
  &amp;lt;label for="user_password"&amp;gt;Password&amp;lt;/label&amp;gt;
  &amp;lt;%= f.password_field :password, :tabindex =&amp;gt; "2" %&amp;gt;
  &amp;lt;br /&amp;gt;
  &amp;lt;label for="user_remember_me"&amp;gt;Remember me:&amp;lt;/label&amp;gt;
  &amp;lt;%= f.check_box :remember_me, :tabindex =&amp;gt; "0" %&amp;gt;
  &amp;lt;br /&amp;gt;
  &amp;lt;%= submit_tag 'Log in', :tabindex =&amp;gt; "3" %&amp;gt;
  &amp;lt;br /&amp;gt;
&amp;lt;% end %&amp;gt;

&amp;lt;%= set_focus_to_id 'user_login' %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When the login form is shown the focus will be set to the login text field.
Of course java script needs to be enambled otherwise they will have to do it manually.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://technorati.com/tag/rails+input+focus" rel="tag"&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 29 Oct 2006 15:40:38 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:eb15f7bc-9053-4188-80b5-1c1739f18a2f</guid>
      <author>Jim Morris</author>
      <link>http://blog.wolfman.com/articles/2006/10/29/setting-the-focus-in-a-form</link>
      <category>Rails</category>
      <category>rails</category>
      <category>focus</category>
      <category>form</category>
      <trackback:ping>http://blog.wolfman.com/articles/trackback/67</trackback:ping>
    </item>
    <item>
      <title>"Setting the focus in a form" by eric</title>
      <description>&lt;p&gt;You can simplify your helper to three lines, thanks to prototype:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def set_focus_to_id(id)
  javascript_tag("$('#{id}').focus()");
end
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Sat, 02 Dec 2006 15:01:48 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:9841e978-7a37-4603-a952-c66405438005</guid>
      <link>http://blog.wolfman.com/articles/2006/10/29/setting-the-focus-in-a-form#comment-43</link>
    </item>
    <item>
      <title>"Setting the focus in a form" by wolfman</title>
      <description>&lt;p&gt;Thats cool thanks, the reason I posted this is because 10 minutes of Googling didn't bring up too many Rails specific methods to do it.&lt;/p&gt;</description>
      <pubDate>Sun, 29 Oct 2006 19:13:39 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:f9131558-406e-4760-aa34-15e7284c670e</guid>
      <link>http://blog.wolfman.com/articles/2006/10/29/setting-the-focus-in-a-form#comment-25</link>
    </item>
    <item>
      <title>"Setting the focus in a form" by Chad H</title>
      <description>&lt;p&gt;You can also use prototype/scriptaculous and type:&lt;/p&gt;

&lt;p&gt;Element.focus('domid');&lt;/p&gt;</description>
      <pubDate>Sun, 29 Oct 2006 16:27:20 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d9a00210-5dc7-423d-be18-4c8f0001e46a</guid>
      <link>http://blog.wolfman.com/articles/2006/10/29/setting-the-focus-in-a-form#comment-20</link>
    </item>
  </channel>
</rss>
