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

no head, no body statement script

Status
Not open for further replies.

westcoaststyle

Programmer
Oct 15, 2001
81
US
I want to run a simple script that will set the focus to the first field in a form when the page loads, that's easy enough. The problem that I'm having is that I can't get to the <HEAD> of the page nor to the <BODY> statement so I can't use the ONLOAD command. Is there another way to run the script on the fly?

Thanks!
 
You could try putting a script section at the bottom of your page before the ending body tag. Like:
<script language=javascript>
document.formname.elementname.focus();
</script>
 
I tried that, but it's not working. BTW, I can't get to the Form Name either so I have to use

document.forms[0].elementname.focus();

Should that still work?
 
Yes, as long as that is the first form on the page it will work. I tested using this sample:
Code:
<html>
<body>
<form name=foo>
<INPUT TYPE=&quot;TEXT&quot; id=&quot;q1&quot; name=&quot;1&quot; SIZE=&quot;3&quot; MAXLENGTH=&quot;3&quot; OnKeyup=&quot;if
(this.value.length==3) document.foo.q2.focus()&quot;> -
<INPUT id=&quot;q2&quot; NAME=&quot;1&quot; TYPE=&quot;TEXT&quot; SIZE=&quot;2&quot; MAXLENGTH=&quot;2&quot; onKeyUp=&quot;if
(this.value.length == 2) document.foo.q3.focus()&quot;> -
<INPUT id=&quot;q3&quot; NAME=&quot;1&quot; TYPE=&quot;TEXT&quot; SIZE=&quot;4&quot; maxlength=&quot;4&quot;>
</form>
<script language=javascript>
  document.forms[0].q1.focus();
</script>
</body>
</html>
 
Does it make a difference that the script is after the form? I'd been putting it before and it makes sense that it would error if the script loads first and trys to set the focus to a field that hasn't loaded yet.

Is that the right thinking? I'll test the theory on my form. Thanks a lot for your help, as stupid as it was. :)
 
Ok, that worked in my test case, but now I've run into another problem:

I don't have control over the field name either and it comes up as CHG_PASSWD_FRM.DEFAULT.LNAME.01

How do I tell the script to look at that field when it as &quot;.&quot;s in the name?
 
Well, not sure about that, but if it's always the first element on the page you can do:
document.forms[0].elements[0].focus();

If it's not the first element, then you can cycle through the elements & find the element where element.name = &quot;thefieldnamehere&quot;, set the focus & leave the element loop.

Hope this helps!
Jessica
 
westcoaststyle,

this should also work anywhere inside the <body>:

<script language=&quot;javascript&quot;>
function whatever() {
// do stuff...
}

document.body.onload = whatever;
</script>
======================================

if (!succeed) try();
-jeff
 
Thank you both for your time and help! I am so appreciative!

I'll check the code with my app and let you know.
 
I got it with the elements[#]. Thank you sooo much! I was pulling my hair out cause it was so simple and I knew it.. haha... Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top