473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
5 8779
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
0
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
0
by: Caesar Augustus | last post by:
I'm having a problem with two different javascript controls in my app. The first chuck of javascript that I pasted into my app is the client-side calendar control popup which works fine when first...
9
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
0
by: Wyatt70 | last post by:
I'm attempting to implement a javascript popup calendar in a DataGrid. The calendar will be used to insert a date into a textbox. I keep getting a "System.NullReferenceException: Object reference...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
11
by: slinky | last post by:
I'm trying to simply set the focus of "txtUserName" upon opening this login form. I put in some Javascript but it is not working. Any ideas? Thanks! <%@ Page Language="vb"...
1
by: vinya | last post by:
<html> <head> <script type="text/javascript"> var element; function test(){ abc = document.getElementById('promptbox').value; document.forms.elements.value = abc; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.