We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29051
    • 9 Posts
    Here’s a method that requires no external Javascript libraries and is straightforward (yet admittedly ugly) to implement:

    All you would need to do is add a single line of Javascript to the select element and set the value of the option elements to the id of the elements you would like to display when the option is selected.

    	
    <!-- Set the value of the option elements to the id of the corresponding elements you want to show when selected -->
    <select name="test" onchange="for(x in this.childNodes){ if( this.childNodes[x].value != null ){document.getElementById(this.childNodes[x].value).style.display = 'none';}} document.getElementById(this.value).style.display = 'block';">
    	<option value="div_1">Test Option 1</option>
    	<option value="div_2">Test Option 2</option>
    	<option value="div_3">Test Option 3</option>
    	<option value="div_4">Test Option 4</option>
    	<option value="div_5">Test Option 5</option>
    </select>
    	<br />
    <!-- Set up the elements you want to toggle based upon your selection, style="display: none;" by default -->
    <div id="div_1" style="display: none">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore... </div>
    <div id="div_2" style="display: none">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
    <div id="div_3" style="display: none">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</div>
    <div id="div_4" style="display: none">Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    <div id="div_5" style="display: none">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium...</div>
    
    


    Steven
      • 18374
      • 69 Posts
      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. smiley