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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I set focus on an Input Box when the page opens? 4

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
Just that, my first page opens where a user inputs a Social Security number. Lets say the input box name is "ss_num". How can I set the focus to that box when the page opens?
 
<body onLoad=&quot;document.myForm.ssn.focus()&quot;>
<form name=&quot;myForm&quot;>
<input name=&quot;ssn&quot;>
</form> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
It's sad that some Americans proudly turn their backs on the very Flag the gives them the opportunity to do just that... - Mike
 
Hi Apollo6
This should work for you, change your body tag to say this:
<body onLoad=&quot;document.getElementById(&quot;ss_num&quot;).focus()&quot;> Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseInt(x.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
For hellTell's code to work, make sure to set the id as well as the name...

<input name=&quot;ss_num&quot; id=&quot;ss_num&quot;> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
It's sad that some Americans proudly turn their backs on the very Flag the gives them the opportunity to do just that... - Mike
 
Good point Mike
Not that my browser cares [wink] Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseInt(x.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
Ok, I'm still not having any luck. Do I have something else out of place that is preventing this from working???
Thanks for the help!!!

Heres the script:


<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&quot;<html>
<head>
<title>Benefit Reporting</title>
<Script Language=&quot;JavaScript&quot;>
var ssn_field_length=0;
function TabNext(obj, event, len, next_field) {
if (event == &quot;down&quot;) {
ssn_field_length=obj.value.length;
}
else if (event == &quot;up&quot;) {
if (obj.value.length != ssn_field_length) {
ssn_field_length=obj.value.length;
if (ssn_field_length == len) {
next_field.focus();
}
}
}
}
</Script>
</head>
<form name=&quot;Main&quot; action=&quot;benefit_data.php&quot; method=&quot;post&quot;>
<body onload=&quot;document.getElementById(&quot;ssn_num1&quot;).focus()&quot;>
<div align=&quot;center&quot;>
<img src=&quot;tnologo.gif&quot; />
</div>
<div align=&quot;center&quot;>
<hr />
<br />
Please enter Social Security:
<table>
<tr>
<td>
<input name=&quot;ss_num1&quot; id=&quot;ss_num1&quot; type=&quot;text&quot; size=&quot;3&quot; maxlength=&quot;3&quot; onkeydown=&quot;TabNext(this,'down',3)&quot; onkeyup=&quot;TabNext(this,'up',3,this.form.ss_num2)&quot; />
-
<input name=&quot;ss_num2&quot; type=&quot;text&quot; size=&quot;2&quot; maxlength=&quot;2&quot; onkeydown=&quot;TabNext(this,'down',2)&quot; onkeyup=&quot;TabNext(this,'up',2,this.form.ss_num3)&quot; />
-
<input name=&quot;ss_num3&quot; type=&quot;text&quot; size=&quot;4&quot; maxlength=&quot;4&quot; onkeydown=&quot;TabNext(this,'down',4)&quot; onkeyup=&quot;TabNext(this,'up',4,this.form.submit)&quot; />
</td>
</tr>
</table>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;SUBMIT&quot;>
</div>
</form>
</body>
</html>
 
Try this instead...


<body onload=&quot;document.Main.ssn_num1.focus()&quot;>
<form name=&quot;Main&quot; action=&quot;benefit_data.php&quot; method=&quot;post&quot;> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
It's sad that some Americans proudly turn their backs on the very Flag the gives them the opportunity to do just that... - Mike
 
D'oh! Feeling stupid today.
I put double quotes within quotes on mine.
Should be:
<body onLoad=&quot;document.getElementById('ss_num1').focus()&quot;> Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseInt(x.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
Hey - I didn't see that either HellTel. Which browser do you use that doesn't care about id vs. name? Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
It's sad that some Americans proudly turn their backs on the very Flag the gives them the opportunity to do just that... - Mike
 
IE6. I just tried it and it didn't care if I put name=, getElementById still worked! [dazed] Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseInt(x.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
[wiggle] Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top