Home All Groups Group Topic Archive Search About

Javascript to Set Focus to TextBox

Author
23 Dec 2005 5:41 PM
Joey
I have a javascript piece that sets focus to a textbox on one of my
webforms. The script works fine for IE: it sets the cursor blinking in
the textbox. In Firefox, however, it doesn't work at all.

Below are two snippets from the page source as pulled directly out of
Firefox after viewing the page:


I. HTML CODE BLOCK FOR TEXTBOX

<td align="right" width="100">
   <input name="ctl00$txtUserName" type="text" maxlength="30"
id="ctl00_txtUserName"
style="border-width:0px;border-style:None;width:100px;" />
</td>

II. SCRIPT CODE BLOCK

<script>var
txtBox=document.getElementById("ctl00_txtUserName");if(txtBox!=null)document.all.ctl00_txtUserName.focus();</script>


There is also another script block immediately following this one.

I have used this code on a couple of other sites, and it has always
worked for both browsers. Now, I am having a problem for some reason.
The only thing new here is that I am now using VS2005 and
ClientScript.RegisterStartupScript instead of VS2003 and
Page.RegisterStartupScript.

Any ideas?

Author
23 Dec 2005 6:15 PM
albert braun
i don't know why the script fails on Firefox browsers, but, if you're
only concerned with setting the focus, you might want to try the
ASP.NET 2.0 control "Focus" method :

    protected void Page_Load(object sender, EventArgs e)
    {
        this.TextBox2.Focus();
    }

hth

seasons greetings,
albert
Author
27 Dec 2005 11:07 AM
Edwin Knoppert
How to select all text in this case?
The tb does not hi-lite it's contents this way.
I might need to resort to js then.. to bad..



Show quote
"albert braun" <bcaguard2000-googlegro***@yahoo.com> schreef in bericht
news:1135361745.040323.150240@z14g2000cwz.googlegroups.com...
>i don't know why the script fails on Firefox browsers, but, if you're
> only concerned with setting the focus, you might want to try the
> ASP.NET 2.0 control "Focus" method :
>
>    protected void Page_Load(object sender, EventArgs e)
>    {
>        this.TextBox2.Focus();
>    }
>
> hth
>
> seasons greetings,
> albert
>
Author
23 Dec 2005 7:33 PM
Bruce Barker
firefox, uses the w3c dom, not the IE proprietary dom (.all collection).
change code to:

<script>
    var txtBox=document.getElementById("ctl00_txtUserName");
    if (txtBox!=null ) txtBox.focus();
</script>


-- bruce (sqlwork.com)


Show quote
"Joey" <joey.pow***@topscene.com> wrote in message
news:1135359666.265595.234510@o13g2000cwo.googlegroups.com...
>I have a javascript piece that sets focus to a textbox on one of my
> webforms. The script works fine for IE: it sets the cursor blinking in
> the textbox. In Firefox, however, it doesn't work at all.
>
> Below are two snippets from the page source as pulled directly out of
> Firefox after viewing the page:
>
>
> I. HTML CODE BLOCK FOR TEXTBOX
>
> <td align="right" width="100">
>   <input name="ctl00$txtUserName" type="text" maxlength="30"
> id="ctl00_txtUserName"
> style="border-width:0px;border-style:None;width:100px;" />
> </td>
>
> II. SCRIPT CODE BLOCK
>
> <script>var
> txtBox=document.getElementById("ctl00_txtUserName");if(txtBox!=null)document.all.ctl00_txtUserName.focus();</script>
>
>
> There is also another script block immediately following this one.
>
> I have used this code on a couple of other sites, and it has always
> worked for both browsers. Now, I am having a problem for some reason.
> The only thing new here is that I am now using VS2005 and
> ClientScript.RegisterStartupScript instead of VS2003 and
> Page.RegisterStartupScript.
>
> Any ideas?
>

AddThis Social Bookmark Button