Form Dependency Manager
Updated: 22/09/06. Added "EMPTY" keyword. Script no longer tramples over existing events on the form elements.
Description: This is a form dependency script for shuttling the display of fields within a complex form, where the visibility of a form field should depend on the value and/or state of another. It's not meant to replace server side scripting for dictating what fields get filled out by the user; after all, JavaScript can easily be manually disabled by the user.
An overview of this script:
-
All dependency logic is defined via the
class
attribute inside the form fields in question, for easy implementation. - Create dependency based on the value (ie: text fields) or state (ie: radio boxes) of another field. Use "OR" or "AND" to create more complex dependencies.
- Degrades well with browsers that have JavaScript disabled.
Demo: Make your selections:
Directions:
Step 1: Add the below code to the <HEAD> section of your page:
The above code references an external .js file, which you need to download: FormManager.js (right click, and select "Save As").
Step 2: Then, add the below sample form to your page:
This is an example web hosting signup form. It offers options of Windows or Linux hosting. If Windows, the client can choose Apache or IIS, and optional frequent crashes. If the client decides that s/he wants IIS, s/he has the option of ASP, and insecurity. The alternative is Apache, which is also available on Linux, as is SSH access. Obviously, the client cannot choose two webserver packages. It also gives an example of how to hide text using a label and hidden form element, and how to depend on a variable-value element.
More info
The script uses a simple syntax to implement dependency amongst form fields, by utilising the class attribute. There are two basic types of statements:
DEPENDS ON name [BEING value] [OR name [BEING value]] CONFLICTS WITH name [BEING value]
Multiple statements can be separated with the "AND"
keyword. "AND"
can also be used to mark the boundary between these
statements and true CSS classes, so the script doesn't interfere with your
stylesheets. There is also a special keyword for use after BEING
:
EMPTY
. Study the
sample HTML code of Step 2 to see how it works, or post any questions in
this forum
thread.