Text box has no focus

J

JCO

I have a text box, on my form that I cant get the focus on. I'm using it as
shown:
<head>
<body onload=document.getPassword.txtPasswordName.focus()>
</head>

Where
getPassword is the form &
txtPasswordName is the text box

Why is this? Anyone know a better way of doing this?
 
M

McKirahan

JCO said:
I have a text box, on my form that I cant get the focus on. I'm using it as
shown:
<head>
<body onload=document.getPassword.txtPasswordName.focus()>
</head>

Where
getPassword is the form &
txtPasswordName is the text box

Why is this? Anyone know a better way of doing this?


You probably can't use "onload=" as the form isn't loaded.

Remove "onload=" and add the following at the end of the page:

<script type="text/javascript">
document.getPassword.txtPasswordName.focus();
</script>
 
R

Randy Webb

McKirahan said:
You probably can't use "onload=" as the form isn't loaded.

Since onload events only happen after the document is loaded (thats what
its for), then the form is indeed loaded.


OP:
One major problem, and may/may not be part of the source of the problem,
is that the body tag goes after the </head> tag, the body is not part of
the head section. Although most browsers error correct soup code and
close the head when it encounters the body tag. Validate HTML before
complaining about javascript.

It also will depend on whether getPassword is the name or the id of the
form (It matters). Same for txtPasswordName.

Validate your code, after its valid, if it still doesn't set focus, post
the minimum code (or a URL) to the offending page.
 
J

JCO

Yes the form name is getPassword and the text box name is txtPasswordName.
As shown:

<FORM NAME="getPassword" onSubmit="return GetUserInput()">
<p align="center">&nbsp;<INPUT TYPE="password" NAME="txtPasswordName"
VALUE="" SIZE=14>&nbsp;
<input type="reset" value="Clear" name="btnClear"></p>
<p align="center">Enter Password</p>

Base on this form, the onload is as shown:
<body onload=document.getPassword.txtPasswordName.focus()>

Also, an important thing to note, the focus is set correctly when I preview
it in the browser. It does not work after the site is published.

Thanks again for the inputs!
 
J

JCO

Nothing I do, puts the cursor in the text box. Seems strange.
I tried moving the onLoad to the bottom .. below the creation of the form.
It made no difference so I put it back at the beginning of the body.

Still not working!
 
J

JCO

Yes, I tried it exactly as you explained and I also looked at the Google
site to read their code.
It is still not working.

Could it have anything to do with the type of text box?
Below is the exact code that I'm using:




<body>
...... other stuff

<!-- jco; This creates a form that contains a text field and 2-buttons to
validate users input -->
<FORM NAME="frmPassword" onEnter="return valForm()">
<!-- jco; Add two buttons and a text field -->
<p align="center">
<input type=password name="txtInput" size="28" onkeypress="onEnter();" ></p>
<p align="center">
<input type="button" value="Enter Password" name="btnEnter"
onclick="valForm();">&nbsp;&nbsp;
<input type="reset" value="Clear" name="btnClear"></p>
</form>

<!-- jco; Setfocus to text box so user can quickly sign in -->
<body onload=document.frmPassword.txtInput.focus()>

</body>

Note:
I just moved the onload to the bottom. Now it works, however, the cursor
keeps blinking. There is animaiton on the top frame. The cursor seems to
blink with the animation.

Should I move the line of code back to the top; Before the form?
Or should I manage the blinking ... somehow?
 
J

JCO

I only have one body tag such as:
<body>
....table stuff here
....form is loaded here, then
<body onload=document.frmPassword.txtInput.focus()>
</body>

The blinking is not consistent. It has the normal cursor blink, which is
okay, then every so often the blinking is interfered with by something else.
It is hard to explain. What is weird is that it all works fine when I
preview it.

I will let you know when I get it posted...thanks
 
R

Randy Webb

mscir said:
JCO said:
I only have one body tag such as:
<body>
...table stuff here
...form is loaded here, then
<body onload=document.frmPassword.txtInput.focus()>
</body>

The blinking is not consistent. It has the normal cursor blink, which is
okay, then every so often the blinking is interfered with by something
else.
It is hard to explain. What is weird is that it all works fine when I
preview it.

I will let you know when I get it posted...thanks


I'd recommend you remove the "<body onload..." line and use this approach:

http://www.projectseven.com/tutorials/qtoots/formfldfocus.htm

<form>
...all of the form code goes above this javascript...

<script type="text/javascript">
// set focus to form field
document.forms[0].MyFormFieldName.focus();
document.forms[0].MyFormFieldName.select();
</script>
</form>

And if its not the first form on the page?

document.forms['formName'].elements['elementName'].focus();
document.forms['formName'].elements['elementName'].select();

Now it doesn't matter anymore.

but if it won't focus directly from the onload, set the onload to a
function and call it onload.
 
J

JCO

This is working. Thanks!
Sorry we had to go back n forth so many times when it ended up to be 2-lines
of code.
Again, thanks.

----- Original Message -----
From: "Randy Webb" <[email protected]>
Newsgroups: comp.lang.javascript
Sent: Saturday, February 14, 2004 11:35 AM
Subject: Re: Text box has no focus

Randy Webb said:
mscir said:
JCO said:
I only have one body tag such as:
<body>
...table stuff here
...form is loaded here, then
<body onload=document.frmPassword.txtInput.focus()>
</body>

The blinking is not consistent. It has the normal cursor blink, which is
okay, then every so often the blinking is interfered with by something
else.
It is hard to explain. What is weird is that it all works fine when I
preview it.

I will let you know when I get it posted...thanks


I'd recommend you remove the "<body onload..." line and use this approach:

http://www.projectseven.com/tutorials/qtoots/formfldfocus.htm

<form>
...all of the form code goes above this javascript...

<script type="text/javascript">
// set focus to form field
document.forms[0].MyFormFieldName.focus();
document.forms[0].MyFormFieldName.select();
</script>
</form>

And if its not the first form on the page?

document.forms['formName'].elements['elementName'].focus();
document.forms['formName'].elements['elementName'].select();

Now it doesn't matter anymore.

but if it won't focus directly from the onload, set the onload to a
function and call it onload.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top