This page has been archived. For the latest news on FormAssembly, go to: http://www3.formassembly.com/blog

The Switch Behavior Explained

Update: Comments are closed for this post, but you can go to the wForms forum if you have any question or comment. Thanks !

 

This post is part of the wForms Documentation. wForms is an open-source javascript library that adds commonly needed behaviors to traditional web forms without the need for any programming skill.

For additional help, visit the wForms forum.

The Switch behavior tackle one of the most common problem in web forms: How to display only the relevant parts of a form and hide the rest.

Let’s say I want you to subscribe to my imaginary newsletter. I need:

  1. Your agreement
  2. Your email

But it doesn’t make sense to ask for your email address if you don’t want the newsletter in the first place, right ? With the Switch behavior I can ask for your email only if you check the ‘I want to…’ box.

Go ahead, check the box:

Newsletter Subscription

It’s time for some definitions:

  • The switch is the checkbox.
    The switch operates on a target which can be turned on and off.
  • The target is the email input field.
    The target is visible only when the switch is activated.
  • Since you can have several switches and targets on one page, we need to identify a switch and its target(s) by name. For example ‘NewsletterOptin’.

Now let’s implement our Switch in 3 small steps .

  1. Enable the wForms extension by adding the following code to the <head> element of your page:

    <script type="text/javascript" src="wforms.js" ></script>

  2. Add the ’switch-NewsletterOptin’ class to your checkbox.

    <input type="checkbox" ... class="switch-NewsletterOptin" />

  3. Add the ‘offstate-NewsletterOptin’ class to your target. Because the target is actually the input field and the label, we need to nest them within a div.

    <div class='offstate-NewsletterOptin‘><label … > … </label> <input … /></div>

The switch is now operational, but we are not done yet.

What happens behind the scene is really simple. When you check the box, the div class turns to ‘onstate-NewsletterOptin‘.
How this visually affects the form really depends on your CSS. If you want the show/hide effect, add these 2 rules to your stylesheet.

.offstate-NewsletterOptin { display: none; }
.onstate-NewsletterOptin { display: block; }

If you want the switch to have a different kind of effect, just change your CSS.

That’s all folks.

Note: The name ‘NewsletterOptin’ is just an example. You can use anything as long as you are consistent: ’switch-somename’, ‘offstate-somename’, ‘onstate-somename’. If you use the form builder you will only get to choose a letter between A and K.

 

Update: Comments are now closed for this post, but you can go to the wForms forum if you have any question or comment. Thanks !

The Form Assembly is Open for Beta-Testing

There is still a lot to be done, but I feel that the project is ready to see some action.

The Form Assembly is a two part project: the wForms extension and the Form Builder.

The wForms Extension

wForms is a javascript extension that adds commonly needed behaviors to traditional web forms. It follows the principle of progressive enhancement : unobtrusive, cross-browser and degradable. I should also point out that not a single line of code is required to actually use it. That makes the learning curve almost non-existant. If you can add a class to a tag, then you can use wForms.

Implemented behaviors are:

  • Switch: Allows you to show/hide relevant parts of a form based on the user inputs.
  • Repeat: Allows parts of a form to be repeated if the user wants to provide more answers.
  • Field Hint: Displays contextual help based on the current input focus.
  • Input Validation: Validates common input types (email, numbers, ..) and displays appropriate error messages.

wForms is not as complete or ambitious as XForms or Web Forms 2.0 are, but at least it works with today’s browsers and is web standards compliant.

The Form Builder

The Form Builder is a tool for creating standard compliant web forms. It integrates the wForms extension and relies on a set of technologies like XML, XMLHttpRequest, XSLT and DOM (a.k.a Ajax) to provide a seamless single-screen experience.

Forms created with the builder are catalogued in the Form Library and are available for everybody else to reuse or adapt.

The Form Garden has not bloomed yet. It will hold a collection of CSS stylesheets suitable for web forms. Feel free to contribute.

Your feedback is much appreciated !

You are currently browsing The Form Assembly weblog archives for March, 2005.

Search the Blog Archive

 

The Form Assembly blog is powered by WordPress ~ Entries (RSS) and Comments (RSS).