Set Focus to a text field in a form

Anything relates to the WWW and web development

Set Focus to a text field in a form

Postby admin » Thu Nov 10, 2005 6:24 pm

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.
User avatar
admin
Site Admin
 
Posts: 938
Joined: Tue Jun 14, 2005 3:41 pm

Return to Website

Who is online

Users browsing this forum: No registered users and 5 guests

cron