Placing the cursor in a textbox

  • Thread starter Thread starter Tina
  • Start date Start date
T

Tina

How is it possible to designate a textbox so that when the .aspx page is
displayed the cursor is already there so the user can just start typing
instead of having to click in the textbox with the mouse first?
thanks,
T
 
JavaScript: document.forms[0].textboxName.focus();

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
you can use Javascript to do it. Put this in your html code:

<script language=javascript>
document.Form1.myTextBox.focus();
</script>

where Form1 is the name of your form, and myTextBox is the name of your
textbox.

you can also use .select instead of .focus if you want to highlight any
existing text in the box.
 
it doesn't work. a snippet is pasted below. What did I do wrong?
thanks,
T


<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
document.Form1.tbEmail.focus();
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="tbEmail" style="Z-INDEX: 100; LEFT: 208px; POSITION:
absolute; TOP: 120px" accessKey="u"
tabIndex="1" runat="server"></asp:textbox><asp:label id="Label1"
style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 120px"
runat="server"> <u>U</u>sername: </asp:label><asp:textbox id="tbPassword"
style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 184px"
accessKey="p" runat="server" TextMode="Password"
Width="152px"></asp:textbox><asp:button id="btnLogin" style="Z-INDEX: 104;
LEFT: 248px; POSITION: absolute; TOP: 248px"

Kevin Spencer said:
JavaScript: document.forms[0].textboxName.focus();

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

Tina said:
How is it possible to designate a textbox so that when the .aspx page is
displayed the cursor is already there so the user can just start typing
instead of having to click in the textbox with the mouse first?
thanks,
T
 
it doesn't work. a snippet is pasted below. What did I do wrong?
thanks,
T


<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
document.Form1.tbEmail.focus();
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="tbEmail" style="Z-INDEX: 100; LEFT: 208px; POSITION:
absolute; TOP: 120px" accessKey="u"
tabIndex="1" runat="server"></asp:textbox><asp:label id="Label1"
style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 120px"
runat="server"> <u>U</u>sername: </asp:label><asp:textbox id="tbPassword"
style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 184px"
accessKey="p" runat="server" TextMode="Password"
Width="152px"></asp:textbox><asp:button id="btnLogin" style="Z-INDEX: 104;
LEFT: 248px; POSITION: absolute; TOP: 248px"
 
Hello Tina,

I have better luck with:

var ctl = document.getElementById('tbEmail');
if (ctl != null) ctl.focus();
 
Back
Top