Kent G
Kent G

Reputation: 1

Rails3 setting focus on a textfield

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

Answers (3)

Doug
Doug

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

gorn
gorn

Reputation: 5340

<%= javascript_tag "$('foo_bar').focus()" %>

Upvotes: 2

Dominic Sayers
Dominic Sayers

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

Related Questions