PDA

View Full Version : problem to put onload-focus on a formfield with numeric name


raf
11-21-2003, 10:16 PM
I'm writing a form-engine to dynamically build and proces forms. The deal is that the formfields get the primary key value from a formfield-table as their name, and i dynamically set the focus on one of the fields. Whitch looks like

<body onload="document.forms[0].elements['56'].focus();">

the formfield then looks like
<input type="text" id="56" name="56" value="bar" />

For some reason, this doesn't work. If i put a character in front of the fieldname, all is fine, but on numerical names, i get an error
like
document.forms[0].elements['56'] is empty or not an object.

Anyone knows a fix for this ?

Vladdy
11-21-2003, 11:15 PM
http://www.w3.org/TR/html4/types.html#type-name

Vincent Puglia
11-22-2003, 06:15 AM
Hi,

What Vladdy wants you to look at is this:

ID and NAME tokens must begin with a letter ([A-Za-z]) ...

So, the fix is: prefix it with a character.

Vinny

Kor
11-22-2003, 09:15 AM
...or use a number, the sequence of the element. If your element is the 56th element of the form, in the sequence from top to bottom, use:

document.forms[0].elements[55].focus();

raf
11-22-2003, 11:05 AM
Thanks for the replys.

I guess that specifying the sequence would mean the least rework. But i would then need to include a counter variable with a different increment for some datatypes (dates get split into three dropdown etc).

So i might as well swallow the pill and add the character to follow naming conventions :(

Anyway, thanks all.