Setting focus in a rhtml view

How do I set focus on a specific form field when I render a partial from
the controller?

I can do it with page[‘name’].focus from an RJS view, but I can’t figure
out how to do it when rendering this rhtml partial from the controller.

I’m rendering a partial with two fields and neither one has focus until
I hit the tab key or put the cursor in a field with the mouse. Sure
would appreciate a pointer !

Thanks in advance,
Bill

How do I set focus on a specific form field when I render a partial from
the
controller?

I can do it with page[‘name’].focus from an RJS view, but I can’t figure
out
how to do it when rendering this rhtml partial from the controller.

I’m rendering a partial with two fields and neither one has focus until
I
hit the tab key or put the cursor in a field with the mouse. Sure would
appreciate a pointer !

Thanks in advance,
Bill


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

tabindex HTML attribute?

Bill W. wrote:

How do I set focus on a specific form field when I render a partial from
the controller?

I can do it with page[‘name’].focus from an RJS view, but I can’t figure
out how to do it when rendering this rhtml partial from the controller.

I’m rendering a partial with two fields and neither one has focus until
I hit the tab key or put the cursor in a field with the mouse. Sure
would appreciate a pointer !

Thanks in advance,
Bill

Simple javascript should do it.

_my_partial.rhtml
<%= text_field ‘foo’, ‘bar’ %>

<%= javascript_tag “$(‘foo_bar’).focus” %>

<%= javascript_tag render(:update) { |page| page[‘foo_bar’].focus } %>

The browser will execute any tags when its inserted via ajax.