Setting the focus on a form?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jadair
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 10:01 pm
Location: Sterling Heights, MI

Setting the focus on a form?

Post by jadair »

How do I set the initial focus on a form? I know it's probably easy. I just don't know how to do it.

For example: I have a page with a set of edit boxes, but I need to jump to a certain edit box initially?

John
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

a little focus for you

Post by phpScott »

If you are using an onload event handler you can within it use

someFormName.someTextFieldName.focus()

with the .focus() doing the work of setting the cursor to that location.
Works well to direct people to the incorrect field when doing client side error checking.

phpScott
jadair
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 10:01 pm
Location: Sterling Heights, MI

Post by jadair »

Thanks.

What is the syntex required for when I want multiple functions to execute onload?

onload="set_my_values() set_form_focus()"
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

multipe onLoad events

Post by phpScott »

What I usually do is if I have multpile functions to call when the page is loading is to create an intialize function that then calls all the other functions I need to run.
function initialize()
{
firstfunction();
secondfunction();
and the like
}

then in the onload event handler I just call the initialize function.
This allows for greater flexability in which functions you might want to call, it centralize them and takes a bunch of code out of the body tag

phpScott
jadair
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 10:01 pm
Location: Sterling Heights, MI

Post by jadair »

Thanks.
Post Reply