View Full Version : Dynamically add form elements

10-03-2006, 11:18 PM
I'm trying to create an order form in the guise of a selection box, with accompanying field for quantity.

The way i would liek it to work is thus:

Select the product from the drop down box. Enter the quantity in the field.

Press the "Add" button to insert another row. For those of you that have used Gmail, its the same effect of adding another attachment, but adding two fields, a selection and a text field. Also, changing the form action to reflect the number of fields. e.g. submit.asp?q=4 for four rows, submit.asp?q=12 for 12 and so on.

Thanks for any help, sorry if this makes no sense... its late and i have a head cold - not a good combination.

10-04-2006, 05:16 AM
Once you add form elements, and if they are named, their value should be passed automatically via submission.

The best way to add form elements is via the DOM:

var newInput=document.createElement('input');

As I'm not sure of all the specifics, here is a very basic working example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function add_input(){
var newInput=document.createElement('input');
<form name="myform" action="#">
<input type="submit" value="submit">
<input type="text" name="art" onchange="add_input();">

10-04-2006, 11:22 PM
Thanks for the reply. I really dont knwo what i'm doing with JS, so if you dont mind i'll pick your brains a little more...

How do i specify the type of form element i'm adding? I want ti to add both a select box with some 50 options or so, and a text field.

I'd also like them to be named select1, text1... select2, text2... select3, text3... etc for ease of processing with the form.

Thanks again,


10-05-2006, 11:12 PM
Anyone? Please? Help?!

10-06-2006, 05:34 AM
That's a lot of creation. To create any particular element, use its tag name. With inputs, the type attribute may be defined (I left that out of my initial script as 'text' appears to be the default type). Children should be created before parents and then appended to their parent:

function add_input(){
var newOption0=document.createElement('option');
newOption0.innerHTML='Twenty Five';
var newOption1=document.createElement('option');
newOption1.innerHTML='Seventy Five';
var newOption2=document.createElement('option');
var newSelect=document.createElement('select');
var newInput=document.createElement('input');

One or more arrays and counting variables could be set up to keep track of the options, selects, and text inputs, their names, values and whatever. Depending upon what each would be, arrays,variables or creation functions could be established for some or all of them so that they would be at the ready when needed rather than having to be individually created from scratch each time.

06-07-2008, 07:21 AM
want to design a html form. it will contain two text areas at the top as a parent.
txt1 textarea
txt2 textarea
add button
whenever i click on add button it should again add two text areas to to the parent. This first child element will contain again add button to add two more text areas (first child's first child element) to first child element. Like this parent element can have any number of child elements, and again these child elements can have any number of child elements. [here each element is considered as two text areas]. There should also be facility to remove these elements. Actually trying to do it but i am not getting how to do it.. Please anyone help me.