# How to set the focus to a specified field in a form

**URL:** <https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907>\
**Category:** Rails\
**Created:** [December 5, 2008, 2:51am UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907 "2008-12-05T02:51:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 2:51am UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/1 "2008-12-05T02:51:49Z")

</div>

This is probably a general HTML question, but I’ll ask it here anyway.

Is there any way to set the focus to a specific field in a form  
generated in  
a view? The little I’ve found on so far suggests that I need to add  
some  
javascript to do this. I can go figure out how to do that, but it seems  
like it should be the sort of thing that could already be built into  
Rails.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 10:51am UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/2 "2008-12-05T10:51:21Z")

</div>

this one requires prototype (for textfield with id “searchtext”):

without prototype it would be something like this:  
document.formname.fieldname.focus();

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/3 "2008-12-05T12:28:14Z")

</div>

On Dec 5, 10:50 pm, Thorsten Müller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> this one requires prototype (for textfield with id “searchtext”):

Does that work for you in Internet Explorer? It didn’t for me. I’m  
currently using a partial to hold my form-focus JavaScript and I pass  
it the ID of the field I require focus for.

#/views/shared/\_form\_focus.html.erb

Then I call that in a view (in this example to pass focus to the field  
with id first\_name):

\<%= render :partial =\> ‘shared/form\_focus’, :locals =\> {:focusField =\>  
‘first\_name’} -%\>

For more discussion about this solution, see  
[http://forums.htmlhelp.com/index.php?act=Print&client=printer&f=12&t=5399](http://forums.htmlhelp.com/index.php?act=Print&client=printer&f=12&t=5399)

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/4 "2008-12-05T12:48:11Z")

</div>

> Does that work for you in Internet Explorer? It didn’t for me. I’m  
> currently using a partial to hold my form-focus JavaScript and I pass  
> it the ID of the field I require focus for.

Sorry, can’t say for sure. At least not after reading the thread you  
linked.  
If I got this right, the problem you mention happens only, if some  
other  
javascript (like alert) is executed same time.

But I can’t test it right now.

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/5 "2008-12-05T14:25:46Z")

</div>

On Fri, Dec 5, 2008 at 4:50 AM, Thorsten MÃ¼ller  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> this one requires prototype (for textfield with id “searchtext”):

Thank you. That did it (for me with Firefox).

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/6 "2008-12-05T18:10:56Z")

</div>

And remember, kids, it's never too late to pick up bad habits. :-) The "language" attribute of the script tag has been deprecated for, oh, what, like TEN YEARS now? Just sayin' ... -- Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/7 "2008-12-05T18:13:17Z")

</div>

On Fri, Dec 5, 2008 at 12:09 PM, Hassan S. \<  
removed\_email\_address@domain.invalid\> wrote:

> And remember, kids, it's never too late to pick up bad habits. :-) The "language" attribute of the script tag has been deprecated for, oh, what, like TEN YEARS now? Just sayin' ...

Ok,  
As one of the “kids” to whom you refer (albeit a rather old, hairy  
“kid”),  
what would be the good habit I would want use instead?

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 5, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/how-to-set-the-focus-to-a-specified-field-in-a-form/154907/8 "2008-12-05T18:42:35Z")

</div>

2008/12/5 Patrick D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> what would be the good habit I would want use instead?

Short answer:

Longer answer: read the spec – that is, develop the habit of looking  
up the tags you’re using rather than relying on potentially incorrect  
or out-of-date online references – [http://www.w3.org/TR/html401/](http://www.w3.org/TR/html401/)

Running your pages through the W3C validator will also point out  
problematic markup, and frequently prevent a lot of styling and JS  
headaches.

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid
