About   -   Contact   -   Purchase   -   Search   -   What's New

Javascript Set Focus to First Field
One thing we take for granted using windows applications is that the focus for the window will normally be in the first edit field waiting for us to begin typing. This is one feature that is lacking from most web applications that I see. This simple JavaScript can be included in your client side page and called from the form onload event. The function takes a single parameter which is the form object where the focus should be set. The function then fins the first visible element that is not disabled or readonly and sets focus to that field. 

Example calling function:
  <BODY onLoad="f_setfocus( fMyForm );">

function f_setfocus( aForm )
	if( aForm.elements[0]!=null) {
		var i;
		var max = aForm.length;
		for( i = 0; i < max; i++ ) {
			if( aForm.elements[ i ].type != "hidden" &&
				!aForm.elements[ i ].disabled &&
				!aForm.elements[ i ].readOnly ) {
				aForm.elements[ i ].focus();
Tested on Browser OK
IE 5 Y
IE 4 Y

Top of Page

Legal Notice

Ken Howe 2011