tags:

views:

111

answers:

4

I’m maintaining a site in ASP, one of the tasks is to set the focus on textbox on a page. Here is what I have tried:

<script type="text/javascript">
<!--
document.psForm['password'].focus();
//AND
document.getElementById("password").focus();
-->
</script>

I didn't think this would work... and it doesn't:

<form id="psForm" action="logonpw.asp" method="post" defaultfocus="password">

This doesn't work:

<body onload="javascript:docuument.psForm.password.focus();">

Here is the form:

<form id="psForm" action="logonpw.asp" method="post">
    <table border="0" cellpadding="5">
  <tr>
    <td>
          Password:
       </td>
    <td>
      <input type="password" name="password" value="<%= password %>" size="50">
        </td>
      </tr>
     </table>
</form>
+1  A: 
<body onload="javascript:docuument.psForm.password.focus();">

should be

<body onload="javascript:document.psForm.password.focus();">

Check spelling...

Otávio Décio
Thanks for catching that, but it didn’t help.
NitroxDM
A: 
<body onload="javascript:document.psForm.password.focus();">

document was misspelled

Glennular
Thanks for catching that, but it didn’t help.
NitroxDM
+2  A: 

a) move

<script type="text/javascript">
<!--
document.psForm['password'].focus();
//AND
document.getElementById("password").focus();
-->
</script>

to bottom of page source.

b) fire code on load

<script type="text/javascript">
<!--
function handleOnLoad(){
document.psForm['password'].focus();
//AND
document.getElementById("password").focus();
}
-->
</script>
...
<body onload="handleOnLoad();">

and by the way, only the second onfocus would do any good.

Greg Ogle
I still had to add an ID="password"Since the body tag is in an include, I now have an error on every other page. Maybe that will help convince the project manager rewrite this app.Combined with Bravax answer I have a workable solution.Thanks!
NitroxDM
Thanks Greg. :) I should have elborated more like you did.nitroxdm why are you getting an error on every other page? You could add a javascript function call to the onload event using JQuery, for example, just for that one page. Thus not effecting the others.
Bravax
Yeppy.. I missed that. The JQuery solution mentioned "$(document).ready(function(){ // Your code here... });" will wait for the entire DOM to be loaded, which prevents problems but may give a perceivable delay in firing.
Greg Ogle
+4  A: 

Try this: Add:

id="password"

to the input tag, then use:

document.getElementById("password").focus();
Bravax
Combined with Greg Ogle answer I have a workable solution.
NitroxDM