How to focus the cursor in an input field when accessing a site

Discussion in 'Javascript' started by Rune Runnestø, Apr 24, 2005.

  1. How do I focus the cursor in the input field 'numberField' when accessing
    this jsp-file (or html-file) ?


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>

    <form action="" method="post">
    <input type="text" name="numberField" size="30" >
    <p><input type="submit" name="buttonAddNumber" value="Add a number">
    <input type="submit" name="buttonRemoveNumber" value="Remove a number">
    </form>

    </body></html>


    Regard
    Rune
     
    Rune Runnestø, Apr 24, 2005
    #1
    1. Advertising

  2. Rune Runnestø

    RobG Guest

    Rune Runnestø wrote:
    > How do I focus the cursor in the input field 'numberField' when accessing
    > this jsp-file (or html-file) ?
    >
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    >
    > <html>
    > <head>
    > <title>Untitled</title>
    > </head>
    > <body>
    >
    > <form action="" method="post">
    > <input type="text" name="numberField" size="30" >
    > <p><input type="submit" name="buttonAddNumber" value="Add a number">
    > <input type="submit" name="buttonRemoveNumber" value="Remove a number">
    > </form>
    >
    > </body></html>


    <body onload="
    if(document.forms[0].elements['numberField'].focus)
    document.forms[0].elements['numberField'].focus();
    ">

    If you give your form a name, then you can substitute that for the
    '0' in forms[0] and your script will not be dependent on the field
    being in the first form.

    e.g. if your form is called "formA", then:

    <body onload="
    if(document.forms['formA'].elements['numberField'].focus)
    document.forms['formA'].elements['numberField'].focus();
    ">

    ....

    <form action="" method="post" name="formA">

    ....

    --
    Rob
     
    RobG, Apr 24, 2005
    #2
    1. Advertising

  3. Rune Runnestø wrote:


    > <body>
    >
    > <form action="" method="post">
    > <input type="text" name="numberField" size="30" >


    <script type="text/javascript">
    document.forms[0].elements.numberField.focus();
    </script>




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 24, 2005
    #3
  4. > Code:
    > --------------------
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    >
    > <html>
    > <head>
    > <title>Untitled</title>
    > <SCRIPT TYPE=\"TEXT/JAVASCRIPT\">
    > FUNCTION FNSETFOCUS(ELEMID)
    > { DOCUMENT.GETELEMENTBYID(ELEMID).FOCUS();
    > }
    > </SCRIPT>


    It doesn't work in my browser with UPPERCASE. But this works fine:

    <script language="JavaScript" type="text/javascript">
    function fnSetFocus(elemid){
    document.getElementById(elemid).focus();
    }
    </script>
    Maybe Javascript is case-sensitive.
    Thanks anyway !

    Regards
    Rune

    > </head>
    > <body *onload="fnSetFocus('numField');"*>
    >
    > <form action="" method="post">
    > <input type="text" name="numberField"* id="numField"* size="30" >
    > <p><input type="submit" name="buttonAddNumber" value="Add a number">
    > <input type="submit" name="buttonRemoveNumber" value="Remove a number">
    > </form>
    >
    > </body></html>
    >
    >
    > --------------------
     
    Rune Runnestø, Apr 25, 2005
    #4
  5. Rune Runnestø wrote:

    >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    >>
    >> <html>
    >> <head>
    >> <title>Untitled</title>
    >> <SCRIPT TYPE=\"TEXT/JAVASCRIPT\">
    >> FUNCTION FNSETFOCUS(ELEMID)
    >> { DOCUMENT.GETELEMENTBYID(ELEMID).FOCUS();
    >> }
    >> </SCRIPT>

    >
    > It doesn't work in my browser with UPPERCASE.


    The code above is really FUBAR.

    > But this works fine:
    >
    > <script language="JavaScript" type="text/javascript">


    You can safely omit the deprecated language="JavaScript".

    > function fnSetFocus(elemid){
    > document.getElementById(elemid).focus();


    No need to use document.getElementById(), creating a dependency on IDs and
    the W3C DOM. Use

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

    instead. To support more than one form per document, redesign the method:

    function fnSetFocus(f, el)
    {
    if (f && el
    && (f = document.forms)
    && f.elements
    && (el = f.elements[el])
    && el.focus);
    {
    el.focus();
    }
    }

    This will works with names as well.

    > }
    > </script>
    >
    > Maybe Javascript is case-sensitive.


    It is, and so spelled "JavaScript" :)
    However, this feature applies for all ECMAScript implementations.

    > Thanks anyway !


    Thanks for reading before posting, including ...

    > [Top post]


    .... the newsgroup's FAQ.


    PointedEars
    --
    When the power of love overcomes the love
    of power, the world will know peace.
    -- Jimi Hendrix
     
    Thomas 'PointedEars' Lahn, May 8, 2005
    #5
    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. dmaziuk
    Replies:
    3
    Views:
    612
    Chris Gonnerman
    Jan 25, 2011
  2. JW
    Replies:
    7
    Views:
    141
  3. Zvonko
    Replies:
    1
    Views:
    121
  4. Roger
    Replies:
    3
    Views:
    360
  5. Robert Mark Bram

    field - focus() makes it active but no cursor..

    Robert Mark Bram, Aug 15, 2007, in forum: Javascript
    Replies:
    1
    Views:
    116
    Thomas 'PointedEars' Lahn
    Aug 15, 2007
Loading...

Share This Page