By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

JavaScript: How to set focus on a textbox in a HTML form?

P: n/a
Hi,

I have a aspx page that contains a textbox. When the page
loads, it's always the browser's address bar or other
browser controls has the focus. What I want however, is to
let the textbox have the focus, but I don't know how to do
it. I tried things like:
window.document.Form1.MyTxtBox.focus(); but that didn't
work. Can someone show me how?

Thanks
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
weird, this code should work. Remember, you have to put it on the onLoad
event of the <body> tag.

--
Fadi El-Eter, itoctopus - http://www.itoctopus.com
"feng" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl...
Hi,

I have a aspx page that contains a textbox. When the page
loads, it's always the browser's address bar or other
browser controls has the focus. What I want however, is to
let the textbox have the focus, but I don't know how to do
it. I tried things like:
window.document.Form1.MyTxtBox.focus(); but that didn't
work. Can someone show me how?

Thanks

Nov 18 '05 #2

P: n/a
Here's a small html file containing a working example.

--
Fadi El-Eter, itoctopus - http://www.itoctopus.com
"Fadi El-Eter" <el*****@nospamatall.com> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
weird, this code should work. Remember, you have to put it on the onLoad
event of the <body> tag.

--
Fadi El-Eter, itoctopus - http://www.itoctopus.com
"feng" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl...
Hi,

I have a aspx page that contains a textbox. When the page
loads, it's always the browser's address bar or other
browser controls has the focus. What I want however, is to
let the textbox have the focus, but I don't know how to do
it. I tried things like:
window.document.Form1.MyTxtBox.focus(); but that didn't
work. Can someone show me how?

Thanks




Nov 18 '05 #3

P: n/a
Jon
Hi,
it should work - assuming this is the first (and/or only) form on the page
try it like this
<body onload="document.forms[0].MyTxtBox.focus();">
the second form would be document.forms[1] etc
or you can try
<body onload="document.forms[0].elements[0].focus();">
elements[0] being the first form field elements[1] the second etc....

Jon

"feng" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl...
Hi,

I have a aspx page that contains a textbox. When the page
loads, it's always the browser's address bar or other
browser controls has the focus. What I want however, is to
let the textbox have the focus, but I don't know how to do
it. I tried things like:
window.document.Form1.MyTxtBox.focus(); but that didn't
work. Can someone show me how?

Thanks

Nov 18 '05 #4

P: n/a
This is what I've used in the past on regular html pages, although
I've never tried it on an aspx page:

<body onload="document.Form1.MyTxtBox.focus()">
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 18 '05 #5

P: n/a
Page.RegisterStartupScript("SetFocus", "<script language=""Jscript"" > document.getElementById(""Textbox1"").focus(); </Script>")

--
Cheers!
Rajiv. R
Rajspace.Org

"Fadi El-Eter" <el*****@nospamatall.com> wrote in message news:eV**************@TK2MSFTNGP12.phx.gbl...
Here's a small html file containing a working example.

--
Fadi El-Eter, itoctopus - http://www.itoctopus.com
"Fadi El-Eter" <el*****@nospamatall.com> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
weird, this code should work. Remember, you have to put it on the onLoad
event of the <body> tag.

--
Fadi El-Eter, itoctopus - http://www.itoctopus.com
"feng" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl...
Hi,

I have a aspx page that contains a textbox. When the page
loads, it's always the browser's address bar or other
browser controls has the focus. What I want however, is to
let the textbox have the focus, but I don't know how to do
it. I tried things like:
window.document.Form1.MyTxtBox.focus(); but that didn't
work. Can someone show me how?

Thanks



Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.