Click to See Complete Forum and Search --> : cursor in text box


drtanz
05-17-2007, 08:55 PM
how do i set the cursor to be in a particular text box ready for the person to enter his login name on loading the pagE? thanks

NogDog
05-17-2007, 09:30 PM
Here's one way:

<form action="#" method="post" id="myform" name="myform">
<fieldset>
<label>One: <input type="text" size="10" name="one" id="one"></label><br>
<label>Two: <input type="text" size="10" name="two"></label><br>
<label>Three: <input type="text" size="10" name="three"></label>
</fieldset>
</form>
<script type="text/javascript">
document.myform.one.focus();
</script>

drtanz
05-17-2007, 09:36 PM
hi this is my code:

<form action="" method="post" id="frmLogin">
<table width="400" border="1" cellpadding="2" cellspacing="2">
<tr>
<td>User Id</td>
<td><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="password" id="password" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="btnLogin" type="submit" id="btnLogin" value="Login" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
document.frmLogin.username.focus();
</script>

However it does not work what is the problem? thanks

NogDog
05-17-2007, 09:39 PM
Try using adding name="frmLogin" to the form element. (I think that JS looks at the name values while CSS looks at the ID values.)

drtanz
05-17-2007, 09:42 PM
hmm still does not work, strange

NogDog
05-17-2007, 10:06 PM
I copied and pasted your code, added the name="frmLogin" to the <form> tag, and it worked fine for me in both IE7 and FF2. Did you refresh the browser page to make sure you got the latest version of the file?

Working code:

<form action="" method="post" id="frmLogin" name="frmLogin">
<table width="400" border="1" cellpadding="2" cellspacing="2">
<tr>
<td>User Id</td>
<td><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="password" id="password" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="btnLogin" type="submit" id="btnLogin" value="Login" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
document.frmLogin.username.focus();
</script>

drtanz
05-17-2007, 10:21 PM
ok worked fine now tnx alot!

bradgrafelman
05-18-2007, 01:30 AM
Don't forget to mark this thread resolved.

JPnyc
05-18-2007, 11:59 AM
You can also use the generic method of reference:

document.forms[0].elements[0].focus();


Doesn't work any better than what Dog gave you, but just for future reference you should know all the options.

Kudose
05-18-2007, 08:23 PM
Don't forget:

document.getElementById('username').focus();