RE: newbie q: Form element focus


#1

Using Prototype, just do Form.focusFirstElement();

For more Prototype help see:

http://www.sergiopereira.com/articles/prototype.js.html

Cheers, Jonathan.


#2

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

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.


#3

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/