Setting the focus to a form field

The setCursorFocus snippet in the JSP drawer of the Snippets view is a JavaScriptâ„¢ function that sets the cursor focus to a specified form field on a Web page. It must be placed within a <script> tag in a JSP page. To insert and configure this snippet, follow these directions:

  1. Insert the snippet's code into the source code of the page. For more information, see Inserting EGL code snippets.
  2. Replace [n] with the number of the form field which will receive focus. The form fields are numbered beginning with zero. For example, use [3] to set focus to the fourth field on the page.
  3. Set the form name to form1.
  4. Change the <body> tag of the JSP page to <body onload="setfocus();">.
The code inserted by this snippet is as follows:
function setFocus() {
 document.getElementById('form1').elements[n].select();
 document.getElementById('form1').elements[n].focus(); 
}

Related tasks
Inserting code snippets into EGL and JSP files

Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.