Setting focus in a rhtml view


#1

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


#2

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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#3

tabindex HTML attribute?


#4

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.