ava script tutorial - a new approach by Ken J Ward

JavaScript Tutorial - Creating a Form Dynamically

We can create forms, and other objects dynamically. Below is an example of dynamically creating a form (with a select and button). Use the "Create Form" button to make the new form, which appears between the horizontal lines. .(Code for this page The code page is heavily commented, so only the highlights are mentioned here)


If you don't like my select, you can delete it by pressing the button above.

Disable Buttons

At the start, the "Delete Form" button is disabled, because there isn't a form to delete, so it would cause an error. After clicking the "Create Form" button, it becomes disabled, although multiple clicks would simply add more forms to the page.

Form Contents

The form contains a select, which has items with text and values. When selected, the select will send you to a page. There is also a button which is there for show only, but when clicked shows an alert. So the form has the essential elements, including event handling capacity.

Creating the Form

First we locate the paragraph, which is where we want to position the form. Then we create the form and the select. The next thing I do is to create the onClick event for the select.



myselect = document.createElement("select");

//one way to write a function... you have to write it yourself!

myOnChange = new Function("e", "location.href=myselect.options[myselect.selectedIndex].value");

Now I have the basic elements and I can create the options. The first option does not have a value, so I'll add the second item below:

//second option


theText=document.createTextNode("JavaScript Tutorial II");


//this option has a value, an URL, so we set the value



So, above, I create the OPTION first. Then some text, using createTextNode, and append it to the OPTION. Then I use setAttribute to set the value of the OPTION, and append this to the OPTION.

When I have created all the options I need, then I begin to build up the form:



// the myOnChange(e) function, created earlier, is used to set

//the "onchange" event (Yes, onchange, not onChange!)


I add the select to the form, and the form to my marker, the paragaraph. Lastly, I add the "onchange" event to the select, using the function I created earlier. I have now created the select.

Even though I have made the form, I can make some changes, such as the following, using style or setAttribute:

The style attributes are a throwback from some code to create a table, so some of them, for instance, border, do not work, but do not create errors.





//I need to set the IDs because I want to

// to reference then so I can delete the form later



The most important things about the above are setting the IDs, so I can get a handle on the select later. The second function is to delete the form:

function deleteForm() {

//here I use the IDs set in the create to get a handle on the elements



//so I can remove them:-)


}//end of deleteForm()

I remove the form using removeChild, after I have used the IDs to find it.

Creating Functions

I used two ways to create functions, either could replace the other:

myOnChange = new Function("e", "location.href=myselect.options[myselect.selectedIndex].value");

And, the second function:

function myOnClick(e) {



Normally, I would have used one way or the other, had I not wished to demonstrate the two methods.

Setting text and values in the Options

I used two ways to set the values and options in the code. The first is:

theText=document.createTextNode("JavaScript Tutorial II");


//this option has a value, an URL, so we set the value


The second way was:


//set the text using the innerHTML property instead of adding text node

theOption.innerHTML="Modifying Existing Text";

Naturally, I would normally have been satisfied with either.

Creating a New Paragraph

I created a new paragraph to put the button on a new line:



The new paragraph was added to the form before I added the button.

Creating the Button

The button was created as follow:


//the function here for the button is different from before, to illustrate another

//way to create a function

function myOnClick(e) {



//once again, "onclick" not "onClick"


Setting the Button Value and Size

For IE, it wasn't necessary to set the size, and the value could be simply set, however Netscape wanted the following:

//setting the height and width isn't essential for IE, but is essential for Netscape



//assign the value of the button

theText=document.createTextNode("Click Me");


//the code commented out below works fine for IE, but the above code is needed for

//some other browsers and IE likes it too!

//mybutton.value="Click Me";



So, we create some parents and then some children, which we give appropriate properties, values, etc. Then we systematically add the children to their parents, and the parents to their grand parents, etc.

See the Code Page for more details