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

Discussion in 'ASP .Net' started by feng, Feb 19, 2004.

  1. feng

    feng Guest

    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
     
    feng, Feb 19, 2004
    #1
    1. Advertising

  2. feng

    Fadi El-Eter Guest

    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
     
    Fadi El-Eter, Feb 19, 2004
    #2
    1. Advertising

  3. feng

    Fadi El-Eter Guest

    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

    >
    >
     
    Fadi El-Eter, Feb 19, 2004
    #3
  4. feng

    Jon Guest

    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
     
    Jon, Feb 19, 2004
    #4
  5. feng

    Maureen Guest

    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
     
    Maureen, Feb 19, 2004
    #5
  6. feng

    Rajiv R Guest

    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

    >
    >
     
    Rajiv R, Feb 27, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey
    Replies:
    4
    Views:
    144,625
    Adrian G
    Sep 6, 2011
  2. Replies:
    2
    Views:
    348
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  3. Gruupy
    Replies:
    0
    Views:
    307
    Gruupy
    Aug 15, 2006
  4. Roger
    Replies:
    3
    Views:
    351
  5. Replies:
    1
    Views:
    274
Loading...

Share This Page