text field cursor

Hello,
   I have a simple login window in php. When I open that page, I want the cursor be be in the username field by default and not having to click on the username field in order to type into it.
Please suggest.

here is the form I am using:

<form action="LogIn.php" method="post">
<center>

<table>

<td align="left">
<font size=+1>username:
</td>
<td> <input type="text" name="username" size="20" maxlength="40"><br>
</td>

<tr>
<td>
<font size=+1>password:
</td>
<td>
<input type="password" name="password" size="20" maxlength="40"><br>
</td>
</tr>

</table>
<input type="submit" value="login" name="submit">

</center>
</form>      



kavishshuklaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi kavishshukla,

Change: <body>
To: <body onLoad="document.getElementById('user').focus();">

Change:
<td> <input type="text" name="username" size="20" maxlength="40"><br>
To:
<td> <input type="text" name="username" size="20" maxlength="40" id="user"><br>


Joe P
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RoonaanCommented:
Alternatively you can also use:

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

You will have to make sure that there are no hidden input fields before your first form field.

-r-
0
BogoJokerCommented:
Nice, with Roonaans code you won't need to add the id="user" to your username textfield!
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

BogoJokerCommented:
Roonaan, if you use Firefox and you like extensions would you be interested in a Firefox extension for EE?
http://www.experts-exchange.com/Community_Support/Suggestions/Q_21811687.html
Be the first to post something, even if it is negative... ;)

Joe P
0
waygoodCommented:
I ususally use this javascript function, so there are no errors if there are no forms on the page:-

function FormSetFocus()
{
  if (document.forms.length > 0)
  {
    var TForm = document.forms[0];

    for (i=0;i<TForm.length;i++)
    {
      if ((TForm.elements[i].type=="text")||
       (TForm.elements[i].type=="textarea")||
       (TForm.elements[i].type.toString().charAt(0)=="s"))
      {
        document.forms[0].elements[i].focus();
        break;
      }
    }
  }
}
0
BogoJokerCommented:
Any of these work for you?
Do you need modifications to any?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.