Focus

B

Bruno

How do you set the focus on a certain text field so that as soon as the page
loads up that particular text input box is in focus?
 
G

GD

Bruno said:
How do you set the focus on a certain text field so that as soon as the page
loads up that particular text input box is in focus?

With Javascript.

If the form name is 'tree' and the element name is 'apple' then in the
HEAD element of your page add:

<script type="text/javascript">
onload=function() {
document.tree.apple.focus()
}
</script>


There are other ways to target form elements. This would target the first
element in the first form on your page:

document.forms[0].elements[0].focus()

This is a shortcut version of the above:

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

Search Google for a tutorial about Javascript and DOM!
 
P

Paul Furman

GD said:
With Javascript.
...

<script type="text/javascript">
onload=function() {
document.tree.apple.focus()
}
</script>...

Hmm, yeah, I just found something similar and didn't even have to state
that it was javascript:

<BODY onLoad="document.forms.mail.subject.focus()">

It worked just fine, I guess it wouldn't if the visitor had java script
turned off. Have I done something wrong? Any harm in this?
 
L

Leif K-Brooks

Bruno said:
How do you set the focus on a certain text field so that as soon as the page
loads up that particular text input box is in focus?

Don't do that. If I've already started typing, my cursor will go to the
beginning of the box. If I'm a keyboard user who doesn't wants to use
something above that box, I'll have to tab my way all the way up.
 
P

Paul Furman

Won't it already be there before you start typing though? I've visited
pages where I start typing but the cursor isn't in a box and it drives
me nuts.
 
L

Leif K-Brooks

Paul Furman wrote:

Top-posting fixed. Don't do that.
Won't it already be there before you start typing though? I've visited
pages where I start typing but the cursor isn't in a box and it drives
me nuts.

Yes, because I put it there with my tab key or my mouse. If you can't
figure out how to put your cursor in a box, then you need to learn how
to use your browser.
 
P

Paul Furman

What else would you want to do? alt keys still work for menu tasks.

I don't see the problem. The script loads when the page loads so when
the page loads, your cursor is in the first box. A tab would move to the
second box, a mouse click would go to wherever you clicked unless you
can tab or click faster than java can calculate. I can understand a
larger page with multiple forms if you moved someone to an unexpected
box at the bottom. I don't use tab on web pages unless I'm in a form, I
see now that one tab takes me to the address bar, is this what you are
talking about? When I go to a page with a form, I expect to be able to
start typing and it annoys me to have to move my mouse (after wasting
time typing into nothingness) or tab twice. This seems common sense but
maybe I'm missing something.
 
T

Toby A Inkster

Bruno said:
How do you set the focus on a certain text field so that as soon as the page
loads up that particular text input box is in focus?

Google will tell you. Don't search for anything. Just view source on their
front page.
 
B

Bruno

Paul Furman said:
What else would you want to do? alt keys still work for menu tasks.

I don't see the problem. The script loads when the page loads so when
the page loads, your cursor is in the first box. A tab would move to the
second box, a mouse click would go to wherever you clicked unless you
can tab or click faster than java can calculate. I can understand a
larger page with multiple forms if you moved someone to an unexpected
box at the bottom. I don't use tab on web pages unless I'm in a form, I
see now that one tab takes me to the address bar, is this what you are
talking about? When I go to a page with a form, I expect to be able to
start typing and it annoys me to have to move my mouse (after wasting
time typing into nothingness) or tab twice. This seems common sense but
maybe I'm missing something.

Agreed. That's exactly what's annoying about the page. It's a simple login
page with username and password fields and if your session has expired you
get thrown back to it so it's a pain if you have to pull the mouse pointer
down and click in the username box before you can start typing in your
username.
 
B

Bruno

GD said:
Bruno said:
How do you set the focus on a certain text field so that as soon as the page
loads up that particular text input box is in focus?

With Javascript.

If the form name is 'tree' and the element name is 'apple' then in the
HEAD element of your page add:

<script type="text/javascript">
onload=function() {
document.tree.apple.focus()
}
</script>


There are other ways to target form elements. This would target the first
element in the first form on your page:

document.forms[0].elements[0].focus()

This is a shortcut version of the above:

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

Thanks but I was aware of a javascript method, just wondered if it was
possible in HTML.
Search Google for a tutorial about Javascript and DOM!
See above. This being a newsgroup on HTML, I was looking for an HTML method
so that it would still work for those with javascript turned off.
 
L

Leif K-Brooks

Paul said:
What else would you want to do? alt keys still work for menu tasks.

Tab my way _down_ from the top of the page.
I don't see the problem. The script loads when the page loads so when
the page loads, your cursor is in the first box.

What if I had my cursor somewhere else before the page was fully loaded?
I don't use tab on web pages unless I'm in a form, I
see now that one tab takes me to the address bar, is this what you are
talking about?

No, I'm talking about tabbing my way to links, form boxes, and
everything else focusable.
 
G

GD

Bruno said:
I was aware of a javascript method, just wondered if it was
possible in HTML.

Nope, HTML is just a markup language. It doesn't 'do' anything or
understand events, it just tells the browser what each piece of text is.
Given that all you're doing is focusing on a field when the page loads
it doesn't really matter if Javascript is enabled or not - It's not
going to break the page or affect accessibility when it doesn't work.

But, as you've seen in other replies, some people don't like things like
this :)
 
P

Paul Furman

OK I understand now that on a complex page with other links and
navigation, it will mess some people up (not many though <g>) but on a
simple form page, it makes sense to me and is what most people expect.
 
M

Michael Wilcox

Bruno said:
How do you set the focus on a certain text field so that as soon as
the page loads up that particular text input box is in focus?

As others have mentioned, it's entirely possible to do so in JavaScript (not
HTML) but I wouldn't recommend it. If I'm already typing in the box (or
another box) before the page finishes loading, my entered text will be
selected and overwritten without me knowing it, or my typing will end up in
the wrong box.
 
M

Mark Parnell

<BODY onLoad="document.forms.mail.subject.focus()">

It worked just fine, I guess it wouldn't if the visitor had java script
turned off. Have I done something wrong? Any harm in this?

Yes. It won't happen until the page is finished loading, so if the user
is on a slow connection, or is a fast typist, they may have already
filled that field in, and be on to the next field.

This is especially a problem with login pages, when the user has already
entered their username, and is in the process of entering their
password, when suddenly the focus goes back to the username field.
 
L

Leif K-Brooks

Paul Furman wrote (talking about putting focus in a form element when
the page loads):
OK I understand now that on a complex page with other links and
navigation, it will mess some people up (not many though <g>) but on a
simple form page, it makes sense to me and is what most people expect.

What if I'm already typing in another form box and then you suddenly
switch focus back to another?
 
E

Eric Bohlman

OK I understand now that on a complex page with other links and
navigation, it will mess some people up (not many though <g>) but on a
simple form page, it makes sense to me and is what most people expect.

A *really* simple form page. For example, Google's main search page has
only one input field, and the page isn't image-heavy. But consider a page
with a lot of fields and lots of images. The onLoad event doesn't happen
until the last image (or, if you've sold your soul to the devil, background
sound) has loaded. And that means that someone might well be in the middle
of filling out a form by the time the event occurs, at which point he'll
suddenly find himself overwriting something he typed before (especially if
the script doesn't just change the focus, but selects the content of the
field switched to, meaning that the first character typed will erase the
entire previous contents).
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top