Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Insert Form Fields On Demand

Hiding and revealing content within div tags is often a practical method of displaying only the form fields a user requires. In some situations, however, the number of fields that might be needed is not readily predictable.

Let's take a gift request list as an example. Some users may require only one form field. Others five. And another might require several dozen. Or even a hundred.

Form fields sufficient for the largest conceivable requirement might be coded into hidden divs to be displayed as needed. And hope there won't be exceptions. It would be a lot of coding, all of which the browser has to load and process.

There is another way. Create the form fields on demand, as few and as many as needed.

Insert Form Fields Example

Let's use the previously mentioned gift request list as a working form example.

It is composed of two parts, JavaScript and a form.

First, let's put the JavaScript into the web page. It doesn't matter whether it is in the HEAD or BODY area. For the example, let's have both JavaScript and form near each other in the source code.

<script type="text/javascript">
// Copyright 2008 Bontrager Connection, LLC
// https://www.willmaster.com/

// When no form field name is provided, it is assumed 
//   to be the default name with the default name 
//   increment number appended.

var DefaultName = "gift";
var DefaultNameIncrementNumber = 0;

// No further customizations required.
function AddFormField(id,type,name,value,tag) {
if(! document.getElementById && document.createElement) { return; }
var inhere = document.getElementById(id);
var formfield = document.createElement("input");
if(name.length < 1) {
   DefaultNameIncrementNumber++;
   name = String(DefaultName + DefaultNameIncrementNumber);
   }
formfield.name = name;
formfield.type = type;
formfield.value = value;
if(tag.length > 0) {
   var thetag = document.createElement(tag);
   thetag.appendChild(formfield);
   inhere.appendChild(thetag);
   }
else { inhere.appendChild(formfield); }
} // function AddFormField()
</script>

The JavaScript will work "as is" for the gift list form example. It can be modified for other uses (see "Notes About The Form Field Insertion JavaScript" further below).

For the example, no edits need to be made to the JavaScript.

Here is the example form:

<form>

<table border="0"><tr><td align="right">
Name: <input name="name"><br />
Email: <input name="email">
</td></tr></table>

List gifts, one per field. If another field 
is needed, click the "[more]" link.

<div id="inhere">
<input type="text" name="gift">
</div>

<a href="javascript:AddFormField('inhere','text','','','div')">[more]</a>
<br />
<input type="submit">
</form>

Paste the form into the test web page with the JavaScript. It will work as is.

Before talking about how it works, here is a working example.

Name:
Email:
List gifts, one per field. If another field is needed, click the "[more]" link.
[more]

Whenever the "[more]" link is clicked, another gift item field is created. It allows an apparently infinite number of fields (limited only by available computer resources).

The original gift item field is name="gift". Subsequent gift item fields are named "gift1", "gift2", "gift3", etc. If the "[more]" link is clicked 100 times, the last field will be name="gift100".

When the example form is submitted, it submits with method GET to the same page that contains the form. Because it is method GET, the URL in the browser's address bar will contain the information that was submitted.

A form implemented on a live web page would, of course, submit to appropriate form handling software.

Notes About The Form Field Insertion JavaScript

The following is the technical description of how to use some of the versatility built into the AddFormField() function. This information will be needed if the example is modified.

How To Call Function AddFormField() —

The AddFormField() function can be called with a link or with a form field button. The example uses a link.

Calling it with a form field button may be done this way:

<input 
   type="button" 
   onclick="AddFormField('place','text','aname','avalue','div')" 
   value="Click for more" />

What AddFormField() Can Do —

When AddFormField() is called, it inserts a form field into the web page according to the parameters it is called with.

AddFormField() can be used to insert form fields in different locations on the web page, even in different forms if more than one exists on the page.

Nine different types of input fields can be inserted. The inserted field name and value may be specified. And, any HTML container tag can be inserted with the form field. (An HTML container tag is one that has both an opening and a closing tag. Examples are div, p, ol, and span.)

When the AddFormField() function is called, the field is created accordingly.

The AddFormField() Parameters —

For quick reference while reviewing the parameter descriptions in the list below, here is the example form's AddFormField() function call:

AddFormField('inhere','text','','','div')

Function AddFormField() expects five parameters (id, type, name, value, tag):

  1. The ID of the container.

    A container is an HTML tag such as div, p, span, ol, or h1. The container for the inserted form field must have an id. That id is specified as the first parameter value.

    In the gift list example, the container is a div and the id is 'inhere'.

  2. The type of the field.

    The inserted form field will be an input tag of the type specified here. The type may be:

    1. text
    2. hidden
    3. password
    4. checkbox
    5. radio
    6. file
    7. reset
    8. submit
    9. button

    In the gift list example, the type is 'text'.

  3. The field's name.

    When the form field is created, it must have a name. If null is specified (nothing between the quotes, like in the gift list example), a default name will be provided.

    Unless a different default is specified, the default name is "gift" with a sequential number appended, like "gift1", "gift2", "gift3", etc.

    For advanced users:

    The default field name can be changed in the first section of the JavaScript at variables DefaultName and DefaultNameIncrementNumber.

    Those variables are used when no form field name is specified for the new field.

    In such case, the DefaultName with the DefaultNameIncrementNumber appended is used as the new field's name. (The DefaultNameIncrementNumber value is then automatically incremented.)

    The values of either or both DefaultName and DefaultNameIncrementNumber may be changed.

  4. The field's value.

    If the inserted form field shall have a value, specify the value. Otherwise, specify null (nothing between the quotes).

    The gift list example specifies a null value.

  5. The tag enclosing the field.

    If an HTML container is to be inserted with form field, specify the tag here.

    HTML containers are any content tag that has both an opening and a closing tag. Div, p, and li are examples of HTML containers. However, br and hr are not containers because they have no corresponding closing tag.

    The gift list example specifies that the inserted fields be wrapped in a div tag. The div tag forces a line-break, putting the inserted text fields neatly in a column. Without the div tag (or other tag that forces a line-break), the inserted text fields would be all in a row, wrapped if the length is beyond the right margin.

    Below is the source code of an example that inserts an li tag with the form field.

    The ol tag id="forfields" is the container within which the fields are to be inserted. Inserted fields are wrapped within an li tag. The result is numbered text fields.

    <p>
    List your gift requests.
    </p>
    <form>
    <ol start="0" id="forfields">
    <li><input type="text" name="gift0"></li>
    </ol>
    <a href="javascript:AddFormField('forfields','text','','','li')">[insert a field]</a>
    <input type="submit">
    </form>
    
    

    And here is a working example.

    List your gift requests.

    [insert a field]

A Way To Learn —

A good way to learn to use function AddFormField() and to realize its versatility is to go ahead and try it.

Create a test page and try every variation in this article. Then make up variations of your own to see what happens.

The Web Developer add-on for the Firefox browser has a "View Generated Source" menu item. That will display the source code as generated after form fields have been inserted. It's an excellent method for seeing exactly what HTML code the browser creates when a field is inserted.

Forms with method GET cause the submitted information to be part of the URL in the browser's address bar. It is an excellent way to verify the right information was sent with the right field names.

Inserting Form Fields On Demand

The technique presented in this article shows how to let the form user insert a seemingly infinite number of form fields on demand.

The fields can be inserted in multiple places, for multiple forms, and can be any of various types. Field name and value may be specified. And, container tags can be inserted with the fields.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC