Page 1 of 1

Set focus in a field?

Posted: Wed Jan 15, 2003 4:55 pm
by Bill H
How can I set focus in a specific field in an html form?

I found one post that gave a way to do it if using an onload event handler, but I'm not using such a thing.

The form is a straightforward one generated by php with no java or javascript involved at all.
I would like the cursor to be in the first edit field when the form loads.

Posted: Thu Jan 16, 2003 2:20 am
by twigletmac
Then you need AFAIK some clientside code, a little Javascript just to set the focus is fairly easy to do using the onload event but others may have another solution.

Mac

Posted: Thu Jan 16, 2003 2:34 am
by volka
searched for it at http://www.w3.org/MarkUp/ but found no property to do so.

Got the answer.

Posted: Fri Jan 17, 2003 9:31 am
by Bill H
Got the answer from a javascript forum at http://www.webxpertz.net/forums

You use the body tag, the index of the form on the page, and the name of the input that you want to have the focus.

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

Posted: Fri Jan 17, 2003 9:51 am
by volka
Bill H wrote:I found one post that gave a way to do it if using an onload event handler, but I'm not using such a thing.
n/c ;)

Posted: Sat Jan 18, 2003 8:41 am
by Gen-ik
Use something like this..

Code: Select all

function FocusMe(target)
&#123;
window.form.target.focus(); //target is the name of the input field
&#125;

// to call this function just send it the name of the input you want to focus
// this this... and it should work.

FocusMe('password');
..this is obviously JavaScript :)

Hope this helps.