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

snippet..

	function start_up(){
<% if controller.action_name == 'criar' -%>
		(document.forms[0])[1].focus();
<% elsif controller.action_name == 'alterar' -%>
		(document.forms[0])[2].focus();
<% 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.

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s