Reputation: 1
When I have errors in a form, I would like to set the focus to the first textfield with the error. I've done this with PHP and JavaScript in the past, but I'm not sure how to approach it in rails3.
Thanks in advance
Upvotes: 0
Views: 1422
Reputation: 131
Although many people (in other articles too) suggested:
<%= javascript_tag "$('foo_bar').focus()" %>
When I first tried that, it didn't work for me. But I didn't look into why because the second thing I tried did work:
<%= f.email_field :email, :autofocus => true %>
This article points out that's an HTML5 approach: How to Focus a Form Input...
Upvotes: 2
Reputation: 1793
I had to do
<%= javascript_tag "$('foo_bar').focus()" %>
to get this to work (note the parentheses on the end of the focus
method.
Upvotes: 0