PDA

View Full Version : Form onload focus in XHTML


ianmh
12-24-2003, 01:12 AM
I know how to get a form field to focus using name in a form. But name is now deprecated in XHTML. I’ve tried replacing name with id but that gives a javascript error.

<body onload="document.signon.userName.focus()>

Then

<input id=”signon” …..

Doesn’t work.

Also tried getElementById, but that didn't work either.

Any ideas how to get this to work without using a name in the form.

Thanks

ianmh
12-24-2003, 01:35 AM
Figured it out at w3schools.com

Tried this.

<script type="text/javascript">
function setFocus()
{
document.forms[0].userName.focus();
}
</script>

Is this even possible using id? Anyway, now I have valid XHTML again. :)

Willy Duitt
12-24-2003, 01:55 AM
FWIW:

Assuming username is the first element in the form:


<script type="text/javascript">
function setFocus()
{
document.forms[0].[0].focus();
}
</script>

.....Willy