views:

47

answers:

4

Hi there,

I'm trying to set the default focus on an input box when the page loads (example: google). My page is very simple, yet I can't figure out how to do this.

This is what I've got so far:

<html>
<head>
 <title>Password Protected Page</title>

 <script type="text/javascript">
 function FocusOnInput()
 {
 document.getElementById("PasswordInput").focus();
 }
 </script>

 <style type="text/css" media="screen">
  body, html {height: 100%; padding: 0px; margin: 0px;}
  #outer {width: 100%; height: 100%; overflow: visible; padding: 0px; margin: 0px;}
  #middle {vertical-align: middle}
  #centered {width: 280px; margin-left: auto; margin-right: auto; text-align:center;}
 </style>
</head>
<body onload="FocusOnInput()">
 <table id="outer" cellpadding="0" cellspacing="0">
  <tr><td id="middle">
   <div id="centered">
  <form action="verify.php" method="post">
   <input type="password" name="PasswordInput"/>
  </form>
   </div>
  </td></tr>
 </table>
</body>
</html>

How come that doesn't work while this works fine?

<html>
<head>
<script type="text/javascript">
function FocusOnInput()
{
     document.getElementById("InputID").focus();
}
</script>
</head>

<body onload="FocusOnInput()">
  <form>
       <input type="text" id="InputID">
  </form>
</body>

</html>

Help is much appreciated :-)

+3  A: 
   <input type="password" name="PasswordInput"/>

this should have an Id

<input type="password" id="PasswordInput" name="PasswordInput"/>
Saikios
+1  A: 

And you can use HTML5's autofocus attribute (works in all current browsers except IE - and promised for IE9). Only call your script if it's IE8 or earlier, or an older version of other browsers.

“works in all current browsers” — that really depends on your definition of “all current browsers”.
Paul D. Waite
A: 

OP: Thank you very much, works perfectly. However, I figured I'd also try the HTML5 autofocus and if I just add autofocus after the input parameters it works in Chrome (5.0.375.125) but not in FireFox (3.6.8). I did it like this:

<html>
<body>
  <form>
       <input type="text" id="InputID" autofocus/>
  </form>
</body>
</html>

Am I missing something?

If you have a question, ask a *question*, don't *answer* one with a question.
David Dorward
I agree with David =D
Saikios
Excuse me, I've never used StackOverflow before and was looking for a way to add an in line comment below 408093's, do you consider this worthy of a new question? Sorry.
I think you only get the ability to comment on other people’s answers once you’ve got 50 reputation points, so don’t worry, it’s not your fault. (We can be a bit enthusiastic about enforcing etiquette around here.)
Paul D. Waite
A: 

To answer your answer/question, firefox doesn't yet support autofocus (it will be introduced in Firefox 4).

You're better off using the script you had before, or alternatively, using a script to find inputs with the autofocus attribute set, and set focus to them if the browser doesn't support the feature.

So you'd still have:

<input type="text" id="InputID" autofocus="autofocus"/>

And using jQuery (you'll need to include the jquery script in your <head>):

$(function() {
    var $input = $("[autofocus=autofocus]");
    if ($input.get(0).autofocus != true) $input.focus();
}

This lets browsers that support autofocus like Chrome to handle it by themselves, while the script will give a helping hand for browsers that don't.

box9