How to focus on a text input?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Csaba

    #1

    How to focus on a text input?

    I'd like to have the cursor in the login form's username input text
    field when users load the login page.
    It works fine with the following implementation:
    <body onload="documen t.login.usernam e.focus();">
    <form name="login" method="post" action="<?=$PHP _SELF?>">
    <input type="text" name="username" onLoad="self.fo cus();">

    The problem is that the input field's name cannot be simly "username",
    because it posts an element of an array, therefore the name is
    "upd[username]".

    Unfortunately when the above implementation is modified according to
    the input field's name, I receive a script error.
    <body onload="documen t.login.upd[username].focus();">
    <form name="login" method="post" action="<?=$PHP _SELF?>">
    <input type="text" name="upd[username]"
    onLoad="self.fo cus();">

    I assume I'd need to escape 'upd[username]' on the 'body onload' line
    somehow, but I don't know how.

    Please help.

    Thank you,

    Chuck
  • mscir

    #2
    Re: How to focus on a text input?

    Csaba wrote:
    [color=blue]
    > I'd like to have the cursor in the login form's username input text
    > field when users load the login page.
    > It works fine with the following implementation:
    > <body onload="documen t.login.usernam e.focus();">
    > <form name="login" method="post" action="<?=$PHP _SELF?>">
    > <input type="text" name="username" onLoad="self.fo cus();">
    >
    > The problem is that the input field's name cannot be simly "username",
    > because it posts an element of an array, therefore the name is
    > "upd[username]".
    >
    > Unfortunately when the above implementation is modified according to
    > the input field's name, I receive a script error.
    > <body onload="documen t.login.upd[username].focus();">
    > <form name="login" method="post" action="<?=$PHP _SELF?>">
    > <input type="text" name="upd[username]"
    > onLoad="self.fo cus();">
    >
    > I assume I'd need to escape 'upd[username]' on the 'body onload' line
    > somehow, but I don't know how.
    >
    > Please help.
    > Thank you,
    > Chuck[/color]

    How about:

    <body onload="documen t.login.element s[upd[username]].focus();">

    Comment

    • Csaba

      #3
      Re: How to focus on a text input?

      Thank you for your quick response. Unfortunately it does not work for
      some reason in IE 5.5 and 6.0
      The javascript error is: 'upd' is undefined

      Do you have any other suggestion?

      Thanks again,
      Chuck

      On Wed, 28 Apr 2004 16:14:44 -0700, mscir
      <mscir@access4l ess.com.net.org .uk> wrote:
      [color=blue]
      >Csaba wrote:
      >[color=green]
      >> I'd like to have the cursor in the login form's username input text
      >> field when users load the login page.
      >> It works fine with the following implementation:
      >> <body onload="documen t.login.usernam e.focus();">
      >> <form name="login" method="post" action="<?=$PHP _SELF?>">
      >> <input type="text" name="username" onLoad="self.fo cus();">
      >>
      >> The problem is that the input field's name cannot be simly "username",
      >> because it posts an element of an array, therefore the name is
      >> "upd[username]".
      >>
      >> Unfortunately when the above implementation is modified according to
      >> the input field's name, I receive a script error.
      >> <body onload="documen t.login.upd[username].focus();">
      >> <form name="login" method="post" action="<?=$PHP _SELF?>">
      >> <input type="text" name="upd[username]"
      >> onLoad="self.fo cus();">
      >>
      >> I assume I'd need to escape 'upd[username]' on the 'body onload' line
      >> somehow, but I don't know how.
      >>
      >> Please help.
      >> Thank you,
      >> Chuck[/color]
      >
      >How about:
      >
      ><body onload="documen t.login.element s[upd[username]].focus();">[/color]

      Comment

      • mscir

        #4
        Re: How to focus on a text input?

        Csaba wrote:[color=blue]
        > Thank you for your quick response. Unfortunately it does not work for
        > some reason in IE 5.5 and 6.0
        > The javascript error is: 'upd' is undefined
        >
        > Do you have any other suggestion?
        >
        > Thanks again,
        > Chuck[/color]

        Chuck,

        Please don't top post, it screws up the Q&A order.

        Here's how I did it:

        var upd=new Array('username 1','username2', 'username3');
        var username=1;
        function show(){
        alert(upd[username]);
        }


        <body onload="documen t.login.element s[upd[username]].focus();">

        <form name="login">
        <input type="button" value="show upd[username]" onclick="show() ">
        <input type="text" value="test" length=3>
        <input type="text" value=test" length=3 name="username2 ">
        </form>

        Post your code if you are still having problems,
        Mike

        Comment

        • Csaba Gabor

          #5
          Re: How to focus on a text input?

          I think this is what you're looking for, try it out for size:

          <html><head><ti tle>Array input elem test page</title></head>
          <?php $inputName="upd[username]"; ?>
          <body onload="documen t.login['<?php print $inputName ?>'].focus();">
          <form name="login" method="post"
          action="<?php print $_SERVER['PHP_SELF']; ?>">
          <input type="text" name="<?php print $inputName ?>"><br>
          <?php print "input was: {$_POST['upd']['username']}" ?>
          </body>
          </html>

          Csaba Gábor from Budapest
          PS. Are you Chuck or Csaba? It was disconcerting
          to see someone with my name posting something I
          didn't remember posting.

          "Csaba" <takethispartof f_mny@softhome. net> wrote in message
          news:9hc090501q ratu42o86r07uce 58be2gk70@4ax.c om...[color=blue]
          > I'd like to have the cursor in the login form's username input text
          > field when users load the login page.
          > It works fine with the following implementation:
          > <body onload="documen t.login.usernam e.focus();">
          > <form name="login" method="post" action="<?=$PHP _SELF?>">
          > <input type="text" name="username" onLoad="self.fo cus();">
          >
          > The problem is that the input field's name cannot be simly "username",
          > because it posts an element of an array, therefore the name is
          > "upd[username]".
          >
          > Unfortunately when the above implementation is modified according to
          > the input field's name, I receive a script error.
          > <body onload="documen t.login.upd[username].focus();">
          > <form name="login" method="post" action="<?=$PHP _SELF?>">
          > <input type="text" name="upd[username]"
          > onLoad="self.fo cus();">
          >
          > I assume I'd need to escape 'upd[username]' on the 'body onload' line
          > somehow, but I don't know how.
          >
          > Please help.
          >
          > Thank you,
          >
          > Chuck[/color]


          Comment

          • Csaba

            #6
            Re: How to focus on a text input?

            Thanks both of you for the answers. Both methods work fine.

            P.S. for Csaba: I'm Csaba too, it is just to hard to remember for most
            people, so almost everybody calls me Chuck.

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: How to focus on a text input?

              mscir wrote:
              [color=blue]
              > var upd=new Array('username 1','username2', 'username3');
              > var username=1;
              > function show(){
              > alert(upd[username]);
              > }
              >
              >
              > <body onload="documen t.login.element s[upd[username]].focus();">
              >
              > <form name="login">
              > <input type="button" value="show upd[username]" onclick="show() ">
              > <input type="text" value="test" length=3>
              > <input type="text" value=test" length=3 name="username2 ">
              > </form>[/color]

              That code allows for improvement:

              - Neither the HTML code nor the references in the script are
              standards compliant.
              - "text" is the default value for the "type" attribute of
              the HTML "input" element and can be safely omitted.
              - The script code is error-prone as it is not checked for
              references prior to access.
              <http://pointedears.de. vu/scripts/test/whatami>
              - Globals are used when not necessary, particularly a variable
              for the array index is used when not necessary.
              - A method is used that operates on hard-coded references
              instead on values passed as arguments.
              - Last but not least: It does not solve the problem of the OP
              which is to have bracket property accessors in script code
              where they should not refer to scripting properties but are
              part of the element's name (attribute value), a common error
              when coding PHP with arrays and client-side J(ava)Script,
              possibly described in the FAQ.)

              Refactored source code could look like this:

              J(ava)Script/ECMAScript:

              function focusElement(sF orm, sElement)
              {
              var f, e;
              if (document
              && document.forms
              && (f = document.forms[sForm])
              && f.elements
              && (e = f.elements[sElement])
              && e.focus)
              {
              e.focus();
              }
              }

              HTML:

              <body onload="focusEl ement('login', 'upd[username]')">
              ...
              <form name="login" action="<?=$PHP _SELF?>" method="post" >
              <input name="upd[username]">
              ...
              </form>
              ...
              </body>

              But it is generally a Bad Thing to focus form elements on load of the
              document since it prevents users from scrolling the document, unless
              they have a pointing device at their hands.


              PointedEars

              Comment

              Working...