# Tips and Tricks

**URL:** <https://www.ruby-forum.com/t/tips-and-tricks/81991>\
**Category:** Rails\
**Created:** [January 16, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/tips-and-tricks/81991 "2007-01-16T19:05:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [January 16, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/tips-and-tricks/81991/1 "2007-01-16T19:05:00Z")

</div>

Hi,

I have successfully implemented my first Ruby on Rails application!  
Now, my users are of course wanting piddly little things like:

\*\* Pressing “enter” to save the form - currently when they press  
enter it does not save and moves away from the form causing them to  
have to re-enter data.

\*\* Having the cursor default to the first field in the form when the  
form opens.

Where is a good place to find out how to do little things like this?  
As always, any advice or suggestion is welcome and appreciated!

Thanks,  
Ali

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [January 16, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/tips-and-tricks/81991/2 "2007-01-16T19:21:04Z")

</div>

> \*\* Pressing “enter” to save the form - currently when they press  
> enter it does not save and moves away from the form causing them to  
> have to re-enter data.

Odd… that should just happen by default…

> \*\* Having the cursor default to the first field in the form when the  
> form opens.

Let’s say your form has the attribute name=‘myform’ then you could do  
something like this (after your form ends):

\<%= javascript\_tag “document.myform.elements[0].focus();” %\>

> Where is a good place to find out how to do little things like this?  
> As always, any advice or suggestion is welcome and appreciated!

> **[JavaScript Tutorial](https://www.w3schools.com/js/default.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

[http://www.w3schools.com/htmldom/default.asp](http://www.w3schools.com/htmldom/default.asp)  
[http://www.w3schools.com/dhtml/default.asp](http://www.w3schools.com/dhtml/default.asp)

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

[http://script.aculo.us/](http://script.aculo.us/)

-philip

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [January 17, 2007, 12:51am UTC](https://www.ruby-forum.com/t/tips-and-tricks/81991/3 "2007-01-17T00:51:18Z")

</div>

> Thanks so much for the reply. I tried the code you suggested and the  
> form just does not want to set focus. I tried those sites you sent as  
> well, and tried changing the code to:
> 
> \<%= javascript\_tag “document.form.focusFirstElement(form);” %\>

Is “form” the name of your form? I’ve never used focusFirstElement  
before…

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [January 16, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/tips-and-tricks/81991/4 "2007-01-16T22:46:52Z")

</div>

Thanks so much for the reply. I tried the code you suggested and the  
form just does not want to set focus. I tried those sites you sent as  
well, and tried changing the code to:

\<%= javascript\_tag “document.form.focusFirstElement(form);” %\>

But that didn’t work either. Ruby/Rails frustrates me so much  
sometimes.

I have the line of code in my \_form at the end. Any other suggestions?

Thanks again!! ~Ali

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [January 17, 2007, 2:02am UTC](https://www.ruby-forum.com/t/tips-and-tricks/81991/5 "2007-01-17T02:02:28Z")

</div>

On 1/17/07, Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have the line of code in my \_form at the end. Any other suggestions?
> 
> Thanks again!! ~Ali

Try

document.forms[0].elements[0].focus();

for the first form element of the first form on the page.  
Or, if you want to focus on a particular named input field:

$(‘user\_name’).focus();

This requires protoype.js.

BTW, this has nothing to do with Rails or Ruby. This is really basic  
javascript stuff - there are plenty of really good resources and  
tutorials out there.

Cheers,  
–max
