# Give focus to an input field of a form

**URL:** <https://www.ruby-forum.com/t/give-focus-to-an-input-field-of-a-form/113441>\
**Category:** Rails\
**Created:** [August 31, 2007, 2:45am UTC](https://www.ruby-forum.com/t/give-focus-to-an-input-field-of-a-form/113441 "2007-08-31T02:45:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@anoweb](https://www.ruby-forum.com/u/anoweb)\
**Post date:** [August 31, 2007, 2:45am UTC](https://www.ruby-forum.com/t/give-focus-to-an-input-field-of-a-form/113441/1 "2007-08-31T02:45:13Z")

</div>

Hi,  
I am new to rails and I have setup Typo on my site. One little  
thing bugs me, the login page doesn’t give focus to the “user\_login”  
field. I was wondering what do I need to do to make rails help with  
this? I have seen some simple inline javascript to set the focus,  
sort of like:

document.\<form\_name\>.user\_login.focus()

However the rails view doesn’t have name for the form, so that code  
won’t work. Below is the code for the login.rhtml from Typo:

\<% form\_tag :action=\> “login” do %\>

```
<h3>Typo</h3>

<div class="form admin">
  <% if flash[:notice] %>
    <h4><%= flash[:notice] %></h4>
  <% end %>
  <ul>
    <li>
      <label for="user_login" class="block"><%= _('Username')%>:</

```

label\>

-   
\<%= \_(‘Password’)  
%\>:  
  
  
  
»" class="primary" /\>  

\<%= link\_to "« " + \_(‘Back to the blog’), { :controller  
=\> ‘/articles’ } %\>

  

\<% end %\>

I want to give focus to the user\_login field when the page loads. Any  
ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![anoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@anoweb](https://www.ruby-forum.com/u/anoweb)\
**Post date:** [August 31, 2007, 11:07am UTC](https://www.ruby-forum.com/t/give-focus-to-an-input-field-of-a-form/113441/2 "2007-08-31T11:07:56Z")

</div>

any one?

---

<div class="post-metadata">

**Author:** ![anoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@anoweb](https://www.ruby-forum.com/u/anoweb)\
**Post date:** [August 31, 2007, 11:29am UTC](https://www.ruby-forum.com/t/give-focus-to-an-input-field-of-a-form/113441/3 "2007-08-31T11:29:57Z")

</div>

found the answer elsewhere:  
[http://railsforum.com/viewtopic.php?pid=19180#p19180](http://railsforum.com/viewtopic.php?pid=19180#p19180)
