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!

form fields - setting focus

Status
Not open for further replies.

northernbloke

Programmer
Aug 9, 2000
29
0
0
GB
Probably forgetting something basic here:

I have a form with 4 text fields and I want to set focus to the first. Problem is that this is an array.

<input type=&quot;text&quot; name=&quot;names[]&quot;>

what I was going to do was this:
<body ONLOAD=&quot;document.myform.names[0].focus();&quot;>

this is throwing up an error of 'Null or not an object'

slap me round the face with a DOH! type solution please.

N'Bloke
 
Your name is NOT an array - HTML doesn't have arrays (javasript does).

<body ONLOAD=&quot;document.myform.names[].focus();&quot;> should work, but I'd recommend changing the name of the form field itself. You can name form elements whatever you'd like and still have javascript look at them as an array....

for (x=0; x<document.myForm.elements.length; x++){
elemType = document.myForm.elements[x].type
elemName = document.myForm.elements[x].name
}
-----------------------------------------------------------------
[pc] Be nice. It's only doing what you tell it to do.
mikewolf@tst-us.com
 
<body onLoad=&quot;eval('document.myform.'+names[0]+'.focus()');&quot;>
not sure that will work but you can give it a try. how is the array created. in a function. why not name the fields or the first so this task is simplified Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Thanks,

I was doing it this way to pass it into PHP...
I am taking Mike's advice and changing my naming/processing a bit.

Thanks again.
 
Me being very dumb..

I added a simple Label ID=&quot;doh&quot;
to the field - so I dont need to change any html or PHP coding now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top