Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Javascript to Set Focus to TextBox

Reply
Thread Tools

Javascript to Set Focus to TextBox

 
 
Joey
Guest
Posts: n/a
 
      12-23-2005
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.f ocus();</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?

 
Reply With Quote
 
 
 
 
albert braun
Guest
Posts: n/a
 
      12-23-2005
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

 
Reply With Quote
 
 
 
 
Bruce Barker
Guest
Posts: n/a
 
      12-23-2005
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)


"Joey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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.f ocus();</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?
>



 
Reply With Quote
 
Edwin Knoppert
Guest
Posts: n/a
 
      12-27-2005
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..



"albert braun" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) oups.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
>



 
Reply With Quote
 
Adrian G Adrian G is offline
Junior Member
Join Date: Sep 2011
Posts: 1
 
      09-06-2011
Quote:
Originally Posted by Bruce Barker View Post
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)
[/color]
Thank you Bruce..
 
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
JavaScript: How to set focus on a textbox in a HTML form? feng ASP .Net 5 02-27-2004 10:57 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