putting the cursor in a password field on page load

S

ScooterMX

I have a page that is simply a password field. Doesn't scroll, do anything
other than accept a password, and has a submit button.

How can I automatically place the cursor in this password field when the
page loads?

I think it would be really nice to just have to type the password and hit
enter.
 
D

Dag Sunde

ScooterMX said:
I have a page that is simply a password field. Doesn't scroll, do anything
other than accept a password, and has a submit button.

How can I automatically place the cursor in this password field when the
page loads?

I think it would be really nice to just have to type the password and hit
enter.

<html>
<head>
<script type="text/javascript">

function initialize() {
document.getElementById(passWord).focus();
}

</script>
</head>

<body onLoad="initialize();">
...
<input id="passWord" name="passWord" type="password" />
...
</body>
</html>
 
R

RobG

Dag Sunde wrote:
[...]
function initialize() {
document.getElementById(passWord).focus();
}

Or if you want it to work:


function initialize() {
document.getElementById('passWord').focus();
}

Note use of quotes around "passWord". If you want it to also work in
older versions of IE:

function initialize() {
if (document.getElementById) {
document.getElementById('passWord').focus();
} else if (document.all) {
document.all['passWord'].focus();
}
}
 
S

ScooterMX

function initialize() {
if (document.getElementById) {
document.getElementById('passWord').focus();
} else if (document.all) {
document.all['passWord'].focus();
}
}

Thanks.
 
R

Randy Webb

RobG said:
Dag Sunde wrote:
[...]
function initialize() {
document.getElementById(passWord).focus();
}


Or if you want it to work:


function initialize() {
document.getElementById('passWord').focus();
}

Note use of quotes around "passWord". If you want it to also work in
older versions of IE:

function initialize() {
if (document.getElementById) {
document.getElementById('passWord').focus();
} else if (document.all) {
document.all['passWord'].focus();
}
}

And if you want it to work in any browser that supports the scripting of
forms:

document.forms['formName'].elements['elementName'].focus();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top