Connecting Tech Pros Worldwide Forums | Help | Site Map

Text box has no focus

JCO
Guest
 
Posts: n/a
#1: Jul 20 '05
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?



McKirahan
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Text box has no focus


" JCO" <J.Oliviero@verizon.net> wrote in message
news:UVjWb.4513$1B6.1175@nwrddc03.gnilink.net...[color=blue]
> I have a text box, on my form that I cant get the focus on. I'm using it[/color]
as[color=blue]
> 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?[/color]


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>


Randy Webb
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Text box has no focus


McKirahan wrote:[color=blue]
> " JCO" <J.Oliviero@verizon.net> wrote in message
> news:UVjWb.4513$1B6.1175@nwrddc03.gnilink.net...
>[color=green]
>>I have a text box, on my form that I cant get the focus on. I'm using it[/color]
>
> as
>[color=green]
>>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?[/color]
>
>
>
> You probably can't use "onload=" as the form isn't loaded.[/color]

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/

JCO
Guest
 
Posts: n/a
#4: Jul 20 '05

re: Text box has no focus


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" <hikksnotathome@aol.com> wrote in message
news:DY6dnTGriYcdvrfdRVn-vg@comcast.com...[color=blue]
> McKirahan wrote:[color=green]
> > " JCO" <J.Oliviero@verizon.net> wrote in message
> > news:UVjWb.4513$1B6.1175@nwrddc03.gnilink.net...
> >[color=darkred]
> >>I have a text box, on my form that I cant get the focus on. I'm using[/color][/color][/color]
it[color=blue][color=green]
> >
> > as
> >[color=darkred]
> >>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?[/color]
> >
> >
> >
> > You probably can't use "onload=" as the form isn't loaded.[/color]
>
> 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/
>[/color]


JCO
Guest
 
Posts: n/a
#5: Jul 20 '05

re: Text box has no focus


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" <hikksnotathome@aol.com> wrote in message
news:DY6dnTGriYcdvrfdRVn-vg@comcast.com...[color=blue]
> McKirahan wrote:[color=green]
> > " JCO" <J.Oliviero@verizon.net> wrote in message
> > news:UVjWb.4513$1B6.1175@nwrddc03.gnilink.net...
> >[color=darkred]
> >>I have a text box, on my form that I cant get the focus on. I'm using[/color][/color][/color]
it[color=blue][color=green]
> >
> > as
> >[color=darkred]
> >>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?[/color]
> >
> >
> >
> > You probably can't use "onload=" as the form isn't loaded.[/color]
>
> 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/
>[/color]


mscir
Guest
 
Posts: n/a
#6: Jul 20 '05

re: Text box has no focus


JCO wrote:[color=blue]
> 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![/color]

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>
[color=blue]
>
> "Randy Webb" <hikksnotathome@aol.com> wrote in message
> news:DY6dnTGriYcdvrfdRVn-vg@comcast.com...
>[color=green]
>>McKirahan wrote:
>>[color=darkred]
>>>" JCO" <J.Oliviero@verizon.net> wrote in message
>>>news:UVjWb.4513$1B6.1175@nwrddc03.gnilink.net.. .
>>>
>>>
>>>>I have a text box, on my form that I cant get the focus on. I'm using[/color][/color]
>
> it
>[color=green][color=darkred]
>>>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.[/color]
>>
>>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/[/color][/color]

JCO
Guest
 
Posts: n/a
#7: Jul 20 '05

re: Text box has no focus


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" <mscir@access4less.net> wrote in message
news:102nr00s8vaq122@corp.supernews.com...[color=blue]
> JCO wrote:[color=green]
> > 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[/color][/color]
form.[color=blue][color=green]
> > It made no difference so I put it back at the beginning of the body.
> >
> > Still not working![/color]
>
> 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>
>[color=green]
> >
> > "Randy Webb" <hikksnotathome@aol.com> wrote in message
> > news:DY6dnTGriYcdvrfdRVn-vg@comcast.com...
> >[color=darkred]
> >>McKirahan wrote:
> >>
> >>>" JCO" <J.Oliviero@verizon.net> wrote in message
> >>>news:UVjWb.4513$1B6.1175@nwrddc03.gnilink.net.. .
> >>>
> >>>
> >>>>I have a text box, on my form that I cant get the focus on. I'm using[/color]
> >
> > it
> >[color=darkred]
> >>>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/[/color][/color]
>[/color]


mscir
Guest
 
Posts: n/a
#8: Jul 20 '05

re: Text box has no focus


JCO wrote:
[color=blue]
> 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?[/color]

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

JCO
Guest
 
Posts: n/a
#9: Jul 20 '05

re: Text box has no focus


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" <mscir@access4less.net> wrote in message
news:102qeg2q6dcar7c@corp.supernews.com...[color=blue]
> JCO wrote:
>[color=green]
> > 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();"[/color]
></p>[color=green]
> > <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[/color][/color]
cursor[color=blue][color=green]
> > keeps blinking. There is animaiton on the top frame. The cursor seems[/color][/color]
to[color=blue][color=green]
> > 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?[/color]
>
> 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
>[/color]


mscir
Guest
 
Posts: n/a
#10: Jul 20 '05

re: Text box has no focus


JCO wrote:
[color=blue]
> 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[/color]

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>

Randy Webb
Guest
 
Posts: n/a
#11: Jul 20 '05

re: Text box has no focus


mscir wrote:
[color=blue]
> JCO wrote:
>[color=green]
>> 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[/color]
>
>
> 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>
>[/color]

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/

JCO
Guest
 
Posts: n/a
#12: Jul 20 '05

re: Text box has no focus


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

"Randy Webb" <hikksnotathome@aol.com> wrote in message
news:YZmdnZ-RZdZgP7PdRVn-hA@comcast.com...[color=blue]
> mscir wrote:
>[color=green]
> > JCO wrote:
> >[color=darkred]
> >> 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[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> >> 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[/color]
> >
> >
> > I'd recommend you remove the "<body onload..." line and use this[/color][/color]
approach:[color=blue][color=green]
> >
> > 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>
> >[/color]
>
> 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/
>[/color]


Closed Thread