[thelist] Dynamic form fields with Javascript

Peter-Paul Koch gassinaumasis at hotmail.com
Wed Jan 23 06:04:11 CST 2002


>You could always use nodes, and thw W3C DOM... that should
>work quite well, however I don't know about how you'd do
>something like that on the legacy DOMs.

You can't. W3C DOM is the only option:

var x= document.createElement('input');
x.setAttribute('name','thename');
x.setAttribute('type','hidden');
x.setAttribute('value','YourValue');

But there were some problems with the setting of the type in IE, test it!

>Also, wouldn't it be
>document.forms["main"].elements[document.forms["main"].elements.length].name="newField";..
>no +1? Baring in mind that Javascript arrays are 0 based.

No. The length is always one longer than the index of the last field. An 
array with length of
3 has 3 fields: 0, 1 and 2. So array[array.length] = newValue; always works.

>If you need help with the W3C DOM, then the chapter about
>it, from the JS definitive guide 4th edition is available
>as a sample from O'Reilly. (It mentions PPK's site too, and
>no doubt you'll find something relevant there too).

Not yet about this specific problem, but you can find an example of creating 
elements at
http://www.xs4all.nl/~ppk/js/index.html?importxml.html (ignore the importing 
of the xml, see the building of the table in the second half of the code).

>http://www.oreilly.com/catalog/jscript4/chapter/ch17.html

ppk

_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com





More information about the thelist mailing list