Focus

Discussion in 'HTML' started by Bruno, Feb 12, 2004.

  1. Bruno

    Bruno Guest

    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?
    Bruno, Feb 12, 2004
    #1
    1. Advertising

  2. Bruno

    GD Guest

    Bruno <> wrote:
    > 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!
    GD, Feb 12, 2004
    #2
    1. Advertising

  3. Bruno

    Paul Furman Guest

    GD wrote:

    > Bruno <> wrote:
    >
    >>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.
    > ...
    >
    > <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?
    Paul Furman, Feb 12, 2004
    #3
  4. Bruno wrote:
    > 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.
    Leif K-Brooks, Feb 12, 2004
    #4
  5. Bruno

    Paul Furman Guest

    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.

    Leif K-Brooks wrote:
    > Bruno wrote:
    >
    >> 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.
    Paul Furman, Feb 12, 2004
    #5
  6. Paul Furman wrote:

    Top-posting fixed. Don't do that.

    > Leif K-Brooks wrote:
    >
    >> Bruno wrote:
    >>
    >>> 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.

    >
    > 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.
    Leif K-Brooks, Feb 12, 2004
    #6
  7. Bruno

    Paul Furman Guest

    Leif K-Brooks wrote:

    >>> 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.


    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.
    Paul Furman, Feb 12, 2004
    #7
  8. Bruno wrote:

    > 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.

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
    Toby A Inkster, Feb 12, 2004
    #8
  9. Bruno

    Bruno Guest

    "Paul Furman" <> wrote in message
    news:...
    > Leif K-Brooks wrote:
    >
    > >>> 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.

    >
    > 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.
    Bruno, Feb 12, 2004
    #9
  10. Bruno

    Bruno Guest

    "GD" <> wrote in message
    news:...
    > Bruno <> wrote:
    > > 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.
    Bruno, Feb 12, 2004
    #10
  11. Paul Furman wrote:
    > Leif K-Brooks wrote:
    >
    >>>> 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.

    >
    > 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.

    --
    Proud "php freak that likes to stick to the rules". Actually, Python.
    Leif K-Brooks, Feb 12, 2004
    #11
  12. Bruno

    GD Guest

    Bruno <> wrote:
    > 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 :)
    GD, Feb 12, 2004
    #12
  13. Bruno

    Paul Furman Guest

    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.
    Paul Furman, Feb 12, 2004
    #13
  14. Bruno <> wrote:
    > 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.
    --
    Michael Wilcox
    mjwilco at yahoo dot com
    Essential Tools for the Web Developer - http://mikewilcox.t35.com
    Michael Wilcox, Feb 12, 2004
    #14
  15. Bruno

    Mark Parnell Guest

    On Thu, 12 Feb 2004 08:57:31 -0800, Paul Furman <>
    declared in alt.html:
    >
    > <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.

    --
    Mark Parnell
    http://www.clarkecomputers.com.au
    Mark Parnell, Feb 12, 2004
    #15
  16. 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?
    Leif K-Brooks, Feb 12, 2004
    #16
  17. Bruno

    Eric Bohlman Guest

    Paul Furman <> wrote in news:WAGdnbWU4oKPS7bdXTWc-
    :

    > 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).
    Eric Bohlman, Feb 13, 2004
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    4
    Views:
    1,618
    Jason
    May 7, 2007
  2. Replies:
    2
    Views:
    336
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  3. Roger
    Replies:
    3
    Views:
    322
  4. Jeff

    manual focus vs. focus()

    Jeff, May 20, 2007, in forum: Javascript
    Replies:
    4
    Views:
    178
  5. Spizzat2
    Replies:
    2
    Views:
    177
    Spizzat2
    Jan 9, 2008
Loading...

Share This Page