Tips and Tricks

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

** 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!

http://www.w3schools.com/htmldom/default.asp http://www.w3schools.com/dhtml/default.asp

http://script.aculo.us/

-philip

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

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...

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