Martin said:
Learn about document.createElement and appendChild (or insertBefore) e.g.
var input = document.createElement('input');
input.type = 'text';
input.id = 'input1';
Note that providing an ID here might interfere with existing stylesheets.
However, an ID for a control can be useful if the `for' attribute is used
on a `label' element, so that the control can be activated by activating
its associated label. See below.
Given that the OP asked about extending an *existing* form, it may
be possible that an element of the chosen name already exists, and
so this line or a similar one causes a run-time error in IE. See
var label = document.createElement('label');
label.appendChild(document.createTextNode('Name? '));
label.appendChild(input);
The `input' element should not be the child element of a `label' element;
it should be following it (as next sibling or otherwise), so that the
`for' attribute and the form control activation mechanism works reliably:
<label for="foo">Bar: </label><input ... id="foo">
http://www.w3.org/TR/html401/interact/forms.html#adef-for
someFormElement.appendChild(label);
All DOM methods should of course be feature-tested before being called, and
all element object references should be subject to a type-converting test
before being used.
Such as:
/**
* @see
http://pointedears.de/scripts/types.js
*/
function isMethodType(s)
{
return /\b(function|object)\b/i.test(s);
}
var input, label;
if (isMethodType(typeof document.createElement) && document.createElement
&& (input = document.createElement('input'))
&& ...)
{
...
}
See also
http://pointedears.de/scripts/test/whatami
PointedEars