Forum: Ruby on Rails Setting focus in a rhtml view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-26 04:08
(Received via mailing list)
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-26 15:38
(Received via mailing list)
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
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-10-05 21:55
tabindex HTML attribute?
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2006-10-05 22:34
Bill Walton 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" %>
  <!-- or -->
  <%= javascript_tag render(:update) { |page| page['foo_bar'].focus } %>

The browser will execute any <script> tags when its inserted via ajax.
This topic is locked and can not be replied to.