Return value

Standards information

Remarks

You must perform a second step when you use createElement to create the input element. The createElement method generates an input text box, because that is the default inputtype property. To insert any other kind of input element, first invoke createElement for input, and then set the type property to the appropriate value in the next line of code.

Attributes can be included with the eTag as long as the entire string is valid HTML. To include the NAME attribute at run time on objects created with the createElement method, use the eTag.

Use the eTag to include attributes when form elements are created that will be reset using the reset method or a BUTTON with a TYPE attribute value of reset.

In Microsoft Internet Explorer 4.0, the only new elements you can create are img, area, and option. As of Microsoft Internet Explorer 5, you can create all elements programmatically,
except frame and iframe. The properties of these created elements are read/write and can be accessed programmatically. Before you use new objects, you must explicitly add them to their respective collections or to the document. To insert new elements into the current document, use the insertBefore method or the appendChild method.

Examples

In Internet Explorer, you can specify all the attributes inside the createElement method by using an HTML string for the method argument. The following example demonstrates how to dynamically create two radio buttons utilizing this technique.

<!DOCTYPEhtml><html><head><title>Radio Button DOM Insertion</title></head><body><form><inputtype="button"onclick="createRadioButton()"value="Create two Radio Buttons"/><br/><inputtype="button"onclick="displayHTML(document.body.outerHTML)"value="Click here to see HTML"/></form><divid="displayBox"></div><script>function createRadioButton() {
// Create radio button object with value="First Choice" and then insert this element into the document hierarchy.var newRadioButton = document.createElement('input');
newRadioButton.setAttribute('type', 'radio');
newRadioButton.setAttribute('name', 'radio1');
newRadioButton.setAttribute('value', 'First Choice');
document.body.insertBefore(newRadioButton); // Since the second argument is implicity null, this inserts the radio button at the end of this node's list of children ("this" node refers to the body element).// Create radio button object with value="Second Choice" and then insert this element into the document hierarchy.
newRadioButton = document.createElement('input');
newRadioButton.setAttribute('type', 'radio');
newRadioButton.setAttribute('name', 'radio2');
newRadioButton.setAttribute('value', 'Second Choice');
document.body.insertBefore(newRadioButton); // Insert the radio button at the end of this node's list of children (which is directly after the radio button we just inserted).
}
function displayHTML(htmlString) {
var temp = document.createElement('div');
temp.appendChild(document.createTextNode(htmlString));
document.getElementById('displayBox').innerHTML = "<pre>" + temp.innerHTML + "</pre>";
}
</script></body></html>