<?xml version='1.0' encoding='utf-8' ?>
<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/'>
  <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>
    <description>A programmers Blog about Ruby, Rails and a few other issue</description>
    <language>en-us</language>
    <ttl>40</ttl>
    <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;pre&gt;  &lt;span class=&quot;comment&quot;&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=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;set_focus_to_id&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;punct&quot;&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;
            &amp;lt;script language=&amp;quot;javascript&amp;quot;&amp;gt;
                &amp;lt;!--
                        document.getElementById(&amp;quot;&lt;span class=&quot;expr&quot;&gt;#{id}&lt;/span&gt;&amp;quot;).focus()
                //--&amp;gt;
            &amp;lt;/script&amp;gt;
        &lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;    END&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        &lt;/pre&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=&quot;user_login&quot;&amp;gt;Login&amp;lt;/label&amp;gt;
          &amp;lt;%= f.text_field :login, :tabindex =&amp;gt; &quot;1&quot; %&amp;gt;
          &amp;lt;br /&amp;gt;
          &amp;lt;label for=&quot;user_password&quot;&amp;gt;Password&amp;lt;/label&amp;gt;
          &amp;lt;%= f.password_field :password, :tabindex =&amp;gt; &quot;2&quot; %&amp;gt;
          &amp;lt;br /&amp;gt;
          &amp;lt;label for=&quot;user_remember_me&quot;&amp;gt;Remember me:&amp;lt;/label&amp;gt;
          &amp;lt;%= f.check_box :remember_me, :tabindex =&amp;gt; &quot;0&quot; %&amp;gt;
          &amp;lt;br /&amp;gt;
          &amp;lt;%= submit_tag 'Log in', :tabindex =&amp;gt; &quot;3&quot; %&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=&quot;http://technorati.com/tag/rails+input+focus&quot; rel=&quot;tag&quot;&gt;&lt;/a&gt;&lt;/p&gt;
      </description>
      <author>Jim Morris</author>
      <pubDate>Sun, 29 Oct 2006 15:40:38 -0800</pubDate>
      <link>http://blog.wolfman.com/articles/2006/10/29/setting-the-focus-in-a-form</link>
      <guid isPermaLink='false'>urn:uuid:eb15f7bc-9053-4188-80b5-1c1739f18a2f</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by Chad H</title>
      <description>
        You can also use prototype/scriptaculous and type:
        
        Element.focus('domid');
      </description>
      <pubDate>Sun, 29 Oct 2006 16:27:20 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-52</link>
      <guid isPermaLink='false'>urn:uuid:d9a00210-5dc7-423d-be18-4c8f0001e46a</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by wolfman</title>
      <description>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.</description>
      <pubDate>Sun, 29 Oct 2006 19:13:39 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-53</link>
      <guid isPermaLink='false'>urn:uuid:f9131558-406e-4760-aa34-15e7284c670e</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by eric</title>
      <description>
        You can simplify your helper to three lines, thanks to prototype:
        
            def set_focus_to_id(id)
              javascript_tag(&quot;$('#{id}').focus()&quot;);
            end
      </description>
      <pubDate>Sat, 02 Dec 2006 15:01:48 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-54</link>
      <guid isPermaLink='false'>urn:uuid:9841e978-7a37-4603-a952-c66405438005</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by Julian</title>
      <description>Thanks for sharing this useful little snippet, it slotted into what I'm doing and worked first time!</description>
      <pubDate>Sun, 07 Sep 2008 08:04:52 -0700</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-55</link>
      <guid isPermaLink='false'>urn:uuid:839d3857-ad4f-4f05-a9c3-2d4256ea5039</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by Skip</title>
      <description>
        This worked great until I pointed IE6 at it and the page appeared blank even though the source could be seen and was valid. Finally figured out that resizing the window manually made it visible and so I added:
        
        if (navigator.appVersion &lt; &quot;7&quot;
        &amp;&amp; navigator.appName == &quot;Microsoft Internet Explorer&quot;)
          {
          function winalign()
            {
            window.resizeBy(-10,-10);
            setTimeout(&quot;window.resizeBy(10,10);&quot;,200);
            }
          window.onload=function(){winalign();};
          }
        
        If there is a less ugly way to make this work, I'd love to know.
      </description>
      <pubDate>Mon, 26 Jan 2009 13:11:17 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-238</link>
      <guid isPermaLink='false'>urn:uuid:8840d081-57cd-464d-9422-f79b0750a239</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by wolfman</title>
      <description>
        I suspect this has something to do with the timing of when the script is executed, and whether the page has fully loaded. JQuery has a solution to this with the $(document).ready(function() {
           // do stuff when DOM is ready
         });
        So if you were to put the javascript in that function I suspect it will work even on a broken IE6
      </description>
      <pubDate>Mon, 26 Jan 2009 14:07:36 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-239</link>
      <guid isPermaLink='false'>urn:uuid:d3c3acde-7bce-4f6f-9ae8-ef5dcdd7c18f</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by John</title>
      <description>
        After much googling....your's was the most elegant and simple!
        
        Thanks!
      </description>
      <pubDate>Sun, 17 Jan 2010 08:41:18 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-301</link>
      <guid isPermaLink='false'>urn:uuid:224ec39a-6884-4a56-a24d-23b7e77421bb</guid>
    </item>
    <item>
      <title>"Setting the focus in a form" by Jim</title>
      <description>
        To resolve IE6 issues. Works in other browsers as well.
        
        document.observe(&quot;dom:loaded&quot;, function() {
            $('my_control_id').focus()
        });
      </description>
      <pubDate>Thu, 28 Jan 2010 07:25:44 -0800</pubDate>
      <link>http://blog.wolfman.com/posts/15#comment-310</link>
      <guid isPermaLink='false'>urn:uuid:61103e12-7e9b-44c9-aa22-39937d34cc0e</guid>
    </item>
  </channel>
</rss>
