# RE: newbie q: Form element focus

**URL:** <https://www.ruby-forum.com/t/re-newbie-q-form-element-focus/50323>\
**Category:** Rails\
**Created:** [December 15, 2005, 7:01am UTC](https://www.ruby-forum.com/t/re-newbie-q-form-element-focus/50323 "2005-12-15T07:01:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [December 15, 2005, 7:01am UTC](https://www.ruby-forum.com/t/re-newbie-q-form-element-focus/50323/1 "2005-12-15T07:01:30Z")

</div>

Using Prototype, just do Form.focusFirstElement();

For more Prototype help see:

> **[Using prototype.js v1.5.0](https://www.sergiopereira.com/articles/prototype.js.html)**
>
> Prototype.js documentation

Cheers, Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [December 28, 2005, 5:29am UTC](https://www.ruby-forum.com/t/re-newbie-q-form-element-focus/50323/2 "2005-12-28T05:29:51Z")

</div>

Someone asked how to set the focus on the first form element. Jonathan  
Viney answered:

> Using Prototype, just do Form.focusFirstElement();
> 
> For more Prototype help see:
> 
> [http://www.sergiopereira.com/articles/prototype.js.html](http://www.sergiopereira.com/articles/prototype.js.html)
> 
> Cheers, Jonathan.

Thanks. However, I would like more detail. This javascript function  
takes as an argument the name of the form. I created my form using this  
line:  
\<%= start\_form\_tag(:action =\> “createnewdocument”) %\>

It shows up in HTML as follows:

So, two related questions:

1. How do I get start\_form\_tag() to generate a name for the form?
2. If I can’t do #1, what do I pass to the javascript function? What  
would be the exact code snippet?

Thanks.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [December 28, 2005, 9:57am UTC](https://www.ruby-forum.com/t/re-newbie-q-form-element-focus/50323/3 "2005-12-28T09:57:20Z")

</div>

Dan,

```
Take a look @ http://api.rubyonrails.com/classes/ActionView/Helpers/

```

FormTagHelper.html#M000403 which is the docs for the start\_form\_tag  
helper. It’s a bit confusing, but you can pass in the options as the  
second parameter, and the options are where you can set all the other  
html attributes.

Instead of

\<%= start\_form\_tag(:action =\> “createnewdocument”) %\>

do

\<%= start\_form\_tag( { :action =\> “createnewdocument”) }, { :id =\>  
“MyFormName” } %\>

Note the {}'s around the :action, which is the ‘url\_for\_options’ part  
of the helper.

‘MyFormName’ is the id that you’d pass to the other function.

Chris Nolan.ca  
[http://kekova.ca/](http://kekova.ca/)
