Setting focus to a text box on page load

W

William LaMartin

All of a sudden code that had been working at my websites to set the focus
to a particular text box on page load now produces an error.

The code is:

'put the focus on the first text box
Dim strScript As String
strScript = "<script>"
strScript = strScript & "document.forms[0].txtUserName.focus();"
strScript = strScript & "</script>"
Page.RegisterStartupScript("ClientScript", strScript)

And the error produced is: 'document.forms.0.txtUserName' is null or not an
object

It is happening in my development computer as well at sites. All I can
figure is that some Windows update changed IIS in some way.

What is the solution?
 
W

William LaMartin

After sending the previous message, I changed

strScript = strScript & "document.forms[0].txtUserName.focus();"

to

strScript = strScript & "document.getElementById('txtUserName').focus()"

and the error message went away. Still, something has changed on the
servers to cause the error in the first place.
 
E

Eliyahu Goldin

Replace

document.forms[0].txtUserName.focus();

with

document.getElementById("txtUserName").focus();

Eliyahu
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top