Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get focus on a particular field on Page Load 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
When a page loads ...
How can i get focus on a particular field.(Text box,check box etc.) with the help of JAVASCRIPT.

Thanks in Advance
 
function setFocus(){
document.formName.elementName.focus();
}

<body onLoad=&quot;setFocus();&quot;>

formName being the name of your form, and elementName being the name of the form element you would like to set the focus to...

has it done, and that's where I found it, too. :)
 
You could also use:
Code:
document.forms[0].elements[0].focus();
to set the focus to the first field in the first form, regardless of the form and field names.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks for such a good response.
You both gave me valuable tips .
and I seek same help in future
 
Here it is December 2001, and I am grateful for the question and replies from you three waaaaaaayyyyyy back in March...saved my neck, you did...this is a most helpful site and I appreciate it.

JCroft
--only those that do not try, fail--
 
Cool. I guess that's why they keep threads around so long.

And it's nice to see SOMEONE search for a relevant thread before they post their question! :) Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi all and thanks for your mails,
I don't know so much javascript.
Tsdragon, you said I can access the fields of my doc using:
document.forms[0].elements[0].focus(); etc.

Is there a way I can see all the functions/actions I can invoke on a field?
for example, if my form is frm1 and i have a field &quot;num&quot;
I want to write:

function validate_input(int min, int max)
{
if ((frm1.num.value >= min) && (frm1.num.value <= max))
{

// notify user
frm1.num.reset();
}
}

can I do it? what can I write after the frm1.num. ?
how can I reset the field?
how can I notify the user the best way?

thanks in advance for any fiece of information.
Irit.

 
irit,

you're almost there.

js has loose data typing, so you can't declare your vars as int...though you can name them similarly (intMin, intMax) to keep them straight.

looks like you need to reverse your <= & >= to make your code block execute when the field value is outside your range: if ((frm1.num.value < min) && (frm1.num.value > max))

to reset your field, you can use this:
document.formname.fieldname.value = &quot;&quot;;

to notify the user, an alert is probably easiest:
alert(&quot;I am alerting you!&quot;);

also, if you want execution of your script to halt after the alert, use &quot;return false;&quot;

here's what your function may look like:

function validate_input(intMin, intMax) {
if ((frm1.num.value < intMin) && (frm1.num.value > intMax)) {
// notify user
alert(&quot;Your input is outside the acceptable range.&quot;);
// reset the field
frm1.num.value = &quot;&quot;;
// stop execution
return false;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top