Placing the cursor in a textbox

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
 
K

Kevin Spencer

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

--
HTH,

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

Rob T

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.
 
T

Tina

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
 
T

Tina

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"
 
M

Matt Berther

Hello Tina,

I have better luck with:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top