SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Form focus

  1. #1
    SitePoint Zealot cckrocks's Avatar
    Join Date
    Nov 2000
    Location
    A home in the hills where the grass grows green an
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not a question, but a tidbit. There was someone askig before about how to start off a form with the cursor in a particular text box. Well, this is how to do it (very simple):

    Put this script directly AFTER your form
    Code:
    <script language="JavaScript">
    <!--
    document.form_name.input_name.focus()
    //-->
    </script>
    Just insert the form name and input name, and there it is!
    Think beyond what is common...
    www.jaggedknife.com
    www.fusion-in.com

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case you're a JS newbie of sorts, try this:

    Code:
    <html>
    <head>
    <title>A Page</title>
    <script language="JavaScript">
    <!--
    function changeFocus() {
    document.form1.email.focus()
    }
    //-->
    </script>
    </head>
    
    <body onLoad="changeFocus();">
    <form name="form1" action="somepage.cgi" method="post">
    Email Address: <input type="text" name="email"><br>
    <input type="submit" name="submit" value="Send Email">
    </form>
    </body>
    </html>
    Also, if you wanted to, you could replace "document.form1.email.focus()" with "document.forms[0].email.focus(), I believe. That will automatically locate the first form on the page, regardless of its name.

    I don't know for sure whether you'll still required to specify the NAME attribute in your FORM tag, but it can't hurt either way.

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Heck, if you want to focus on the first field of the first form on the page, you can use:

    document.forms[0].elements[0].focus();
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •