Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > JavaScript: How to set focus on a textbox in a HTML form?

Reply
Thread Tools

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

 
 
feng
Guest
Posts: n/a
 
      02-19-2004
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
 
Reply With Quote
 
 
 
 
Fadi El-Eter
Guest
Posts: n/a
 
      02-19-2004
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" <> wrote in message
news:12d2201c3f6f3$5d490c20$...
> 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



 
Reply With Quote
 
 
 
 
Fadi El-Eter
Guest
Posts: n/a
 
      02-19-2004
Here's a small html file containing a working example.

--
Fadi El-Eter, itoctopus - http://www.itoctopus.com
"Fadi El-Eter" <> wrote in message
news:...
> 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" <> wrote in message
> news:12d2201c3f6f3$5d490c20$...
> > 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

>
>





 
Reply With Quote
 
Jon
Guest
Posts: n/a
 
      02-19-2004
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" <> wrote in message
news:12d2201c3f6f3$5d490c20$...
> 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



 
Reply With Quote
 
Maureen
Guest
Posts: n/a
 
      02-19-2004
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
 
Reply With Quote
 
Rajiv R
Guest
Posts: n/a
 
      02-27-2004
Page.RegisterStartupScript("SetFocus", "<script language=""Jscript"" > document.getElementById(""Textbox1"").focus(); </Script>")

--
Cheers!
Rajiv. R
Rajspace.Org

"Fadi El-Eter" <> wrote in message news:...
Here's a small html file containing a working example.

--
Fadi El-Eter, itoctopus - http://www.itoctopus.com
"Fadi El-Eter" <> wrote in message
news:...
> 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" <> wrote in message
> news:12d2201c3f6f3$5d490c20$...
> > 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

>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
Why does putting focus on textbox also set focus to submit jw56578@gmail.com Javascript 2 06-06-2005 08:20 PM
Set focus on textbox Saifee ASP .Net 4 12-08-2003 07:16 AM
how to set focus on a textbox after onclick of a button sumit ASP .Net 1 11-08-2003 07:40 AM
Set focus on a TextBox mg ASP .Net 2 07-14-2003 01:53 PM



Advertisments