Cursor on the Username text box

  • Thread starter Thread starter Renuka
  • Start date Start date
R

Renuka

I have a Login Page with a Username text box and another one for
Password. How can I have the cursor in the Username text box once the
page loads.
Do I need to have a client side script??

Please help

Thanks in advance
Renuka
 
How can I use it in ASP.net I tried the following javascript in the head
of Login.aspx

<script language="Javascript">
function setFocus()
{
document.forms.txtLogin.focus();
}
</script>

where txtLogin is the id of the asp:Label. And then calling this
function in the body.

<body onLoad="setFocus()">

Please help it gives an error on the line
document.forms.txtLogin.focus();
The error is : Microsoft JScript runtime error:
'document.forms.txtLogin' is null or not an object

Thanks for your help.
Renuka
 
ASP.NET reserves the right to mangle IDs wherever it deems it appropriate.
When you open your page, go to view -> source and look for the <input
type="text" /> that is rendered for the control you are trying to set the
focus on, and then use this value. Alternately, an even better solution is
to have your server response.write the UniqueID of this control, which
guarantees that future name mangling schemes of the web controls will not
break your script.
 
document.forms is a collection. Assuming your form is called "Form1" the
following should work:

document.forms["Form1"].elements["txtLogin"].focus();

Regards,

Mun
 
Back
Top