# Setting focus in a rhtml view

**URL:** <https://www.ruby-forum.com/t/setting-focus-in-a-rhtml-view/58808>\
**Category:** Rails\
**Created:** [April 26, 2006, 4:08am UTC](https://www.ruby-forum.com/t/setting-focus-in-a-rhtml-view/58808 "2006-04-26T04:08:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [April 26, 2006, 4:08am UTC](https://www.ruby-forum.com/t/setting-focus-in-a-rhtml-view/58808/1 "2006-04-26T04:08:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [April 26, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/setting-focus-in-a-rhtml-view/58808/2 "2006-04-26T15:38:38Z")

</div>

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](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [October 5, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/setting-focus-in-a-rhtml-view/58808/3 "2006-10-05T21:55:07Z")

</div>

tabindex HTML attribute?

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [October 5, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/setting-focus-in-a-rhtml-view/58808/4 "2006-10-05T22:34:24Z")

</div>

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.
