views:

39

answers:

2

I'm trying to add a text field to a form dynamically using javascript. From what I can tell, my code should work.

function change()
{
textField = document.createElement("text");
textField.id="sub"+count;
textField.name="sub"+count;
textField.value="Enter a name for another new subcategory";
textField.size="35";
textField.onchange="javascript:change()";
textField.onFocus="javascript:clearField()";
document.getElementById("addCatForm").appendChild(textField);
}
+2  A: 

You want:

var field = document.createElement('input');
field.type = 'text';

NB if you're doing lots of Javascript development, you might want to use a framework, such as ExtJS

The code to do it would be:

var field = Ext.get('form').createChild({
    tag: 'input'
    //other options
});
field.on('change', change);
field.on('focus', focus);
Evan Trimboli
You may want to use "setAttribute" so `field.setAttribute("type", "text")`.
The Who
Thanks to both of you! It works great now.
Jimmy
+1  A: 

You're creating a TEXT element, but adding things to it for an INPUT element.

Peter Ajtai