By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,345 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Text box has no focus

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
" JCO" <J.********@verizon.net> wrote in message
news:UV*****************@nwrddc03.gnilink.net...
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>
Jul 20 '05 #2

P: n/a
McKirahan wrote:
" JCO" <J.********@verizon.net> wrote in message
news:UV*****************@nwrddc03.gnilink.net...
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.


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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #3

P: n/a
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!

"Randy Webb" <hi************@aol.com> wrote in message
news:DY********************@comcast.com...
McKirahan wrote:
" JCO" <J.********@verizon.net> wrote in message
news:UV*****************@nwrddc03.gnilink.net...
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.


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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #4

P: n/a
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!

"Randy Webb" <hi************@aol.com> wrote in message
news:DY********************@comcast.com...
McKirahan wrote:
" JCO" <J.********@verizon.net> wrote in message
news:UV*****************@nwrddc03.gnilink.net...
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.


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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #5

P: n/a
JCO wrote:
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!
Try the approach google uses (view source - relevant code shown).

<html>
<head>
<title>Google</title>
<script>
<!--
function sf(){document.f.q.focus();}
// -->
</script>
</head>
<body onLoad=sf()>
<form action="/search" name=f>
<input maxLength=256 size=55 name=q value="">
</body>
</html>

"Randy Webb" <hi************@aol.com> wrote in message
news:DY********************@comcast.com...
McKirahan wrote:
" JCO" <J.********@verizon.net> wrote in message
news:UV*****************@nwrddc03.gnilink.net.. .
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.


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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/


Jul 20 '05 #6

P: n/a
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?


"mscir" <ms***@access4less.net> wrote in message
news:10*************@corp.supernews.com...
JCO wrote:
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!


Try the approach google uses (view source - relevant code shown).

<html>
<head>
<title>Google</title>
<script>
<!--
function sf(){document.f.q.focus();}
// -->
</script>
</head>
<body onLoad=sf()>
<form action="/search" name=f>
<input maxLength=256 size=55 name=q value="">
</body>
</html>

"Randy Webb" <hi************@aol.com> wrote in message
news:DY********************@comcast.com...
McKirahan wrote:

" JCO" <J.********@verizon.net> wrote in message
news:UV*****************@nwrddc03.gnilink.net.. .
>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.

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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #7

P: n/a
JCO wrote:
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:

<FORM NAME="frmPassword" onEnter="return valForm()">
<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>
<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?


2 questions:

- Do you have two <body> tags?
- Are you unhappy because the cursor blinks in the text box, or with the
RATE of blinking?

Not sure why this didn't work when you left the body tag where it
belongs, just following the <head> section:

<body onload=document.frmPassword.txtInput.focus()>

Maybe it has something to do with other code on the page.

How about you post your page somewhere where folks can view it? If you
don't have a site to publish it to you can easily get a free Geocities
web space after creating a Yahoo email account.

Mike

Jul 20 '05 #8

P: n/a
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
"mscir" <ms***@access4less.net> wrote in message
news:10*************@corp.supernews.com...
JCO wrote:
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:

<FORM NAME="frmPassword" onEnter="return valForm()">
<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>
<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?


2 questions:

- Do you have two <body> tags?
- Are you unhappy because the cursor blinks in the text box, or with the
RATE of blinking?

Not sure why this didn't work when you left the body tag where it
belongs, just following the <head> section:

<body onload=document.frmPassword.txtInput.focus()>

Maybe it has something to do with other code on the page.

How about you post your page somewhere where folks can view it? If you
don't have a site to publish it to you can easily get a free Geocities
web space after creating a Yahoo email account.

Mike

Jul 20 '05 #9

P: n/a
JCO wrote:
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/tutorial...rmfldfocus.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>

Jul 20 '05 #10

P: n/a
mscir wrote:
JCO wrote:
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/tutorial...rmfldfocus.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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #11

P: n/a
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" <hi************@aol.com>
Newsgroups: comp.lang.javascript
Sent: Saturday, February 14, 2004 11:35 AM
Subject: Re: Text box has no focus

"Randy Webb" <hi************@aol.com> wrote in message
news:YZ********************@comcast.com...
mscir wrote:
JCO wrote:
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/tutorial...rmfldfocus.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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.