Satisfaction II – javascript field focus

I Happiness to share 😀

You know when users gotta insert a lot of records into the DB, it is cool if the programmer always set the first field of a Form with focus! Or else they could get really annoyed..

First of all, this is a Rails solution but can be done in any other language.. if you know what page your user is visiting..


	function start_up(){
<% if controller.action_name == 'criar' -%>
<% elsif controller.action_name == 'alterar' -%>
<% end -%>
return true;

then call the function on BODY onload= ‘start_up’

This if elsif is required because in Rails a ‘new’ form have 1 hidden field and a ‘edit’ form have 2 hidden fields, if your form is in PHP C# and have no hidden fields, you are likely to use (document.forms[0])[0].focus(); for the very first field on the very first form.

So COOL !! This function becomes something different depending on the page the user is visiting 😀

Sorry the syntax is messed up is because of word press post security is kinda paranoid..


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s