I agree with ZAP although I haven’t used that particular plugin he referenced.
jQuery makes it a
breeze to show/hide fields selectively based on user input. If your form markup is well-written with lots of hooks it’s actually pretty fun to see just how much flexibility you have.
Here’s a sample from a previous project (I’ve just inlined the jQuery and simplified the form but you get the idea).
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function() {
$("#location").change(function() {
if($("#yes-location").is(":checked")) {
$("#for-state").show();
} else {
$("#for-state").hide();
}
});
// only fires when state is visible (can't click otherwise)
$("#state").change(function() {
// show/hide other fields as needed
});
});
/* ]]> */
</script>
<form id="location-form" action="#" method="post">
<fieldset>
<dl id="location" class="yes-or-no-question">
<dt>Do you live in the US? <span class="required">*</span></dt>
<dd id="for-yes-location" class="has-input-type-radio">
<label for="yes-location">
<span class="label-title">Yes<span class="accessibility">, I live in the US.</span></span>
<input type="radio" class="type-radio" name="location" value="YES" id="yes-location" />
</label>
</dd><!-- /#for-yes-location.has-input-type-radio -->
<dd id="for-no-location" class="has-input-type-radio">
<label for="no-location">
<span class="label-title">No<span class="accessibility">, I do not live in the US.</span></span>
<input type="radio" class="type-radio" name="location" value="NO" id="no-location" />
</label>
</dd><!-- /#for-no-location.has-input-type-radio -->
</dl><!-- /#location -->
<label for="state" id="for-state" class="has-select">
<span class="label-title">State: <span class="required">*</span></span>
<select name="state" id="state">
<option value="">Select your state</option>
<option value="AL">Alabama</option>
</select><!-- /#state -->
</label><!-- /#for-state.has-select -->
</fieldset>
<ul class="actions">
<li><input type="submit" class="type-submit" value="Submit" name="submit" /></li>
<li class="loading"></li>
</ul><!-- /.actions -->
<form><!-- /#location-form -->
... as you can see I really get granular with class names (since IE doesn’t support attribute selectors) but it also lets me really hook into a lot of stuff with my jQuery. Fortunately TextMate bundles make it easy to get so picky.