Page 1 of 1

Set Focus to a text field in a form

PostPosted: Thu Nov 10, 2005 6:24 pm
by admin
This help you set focus to a choosen text field automatically when the page is loaded. This mean that as soon as the page is loaded the user can start typing without having to first click in the text field box first.

Here is the example:
<form name="myform">
<input type="text" name="mytextfield1">
<input type="text" name="mytextfield2">
</form>


To give the mytextfield1 above focus when the page is loaded, use the OnLoad() command in the BODY tag as follow:

<body OnLoad="document.myform.mytextfield1.focus();">

That's all


To automatically give focus to mytextfield2 when mytextfield1 has met the condition:
<form name="myform">
<input type="text" name="mytextfield1" onKeyUp="if(this.value.length==14) this.form.mytextfield2.focus()">
<input type="text" name="mytextfield2">
</form>


In the above example, mytextfield1 is being verify everytime an key is pressed, when 14 keys are pressed, the focus automatically jump to mytextfield two. Hence the user does not have to press the tab key or click on the next text box.