Set focus in a field?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Set focus in a field?

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

searched for it at http://www.w3.org/MarkUp/ but found no property to do so.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Got the answer.

Post 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()">
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
Post Reply