<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tony Thomas &#187; html forms jquery</title>
	<atom:link href="http://anthonygthomas.com/tag/html-forms-jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://anthonygthomas.com</link>
	<description>Father to two, husband to one, web developer and musician.</description>
	<lastBuildDate>Fri, 27 Apr 2012 19:26:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Display Form Fields Based on Selection Using JQuery</title>
		<link>http://anthonygthomas.com/2010/03/14/display-form-fields-based-on-selection-using-jquery/</link>
		<comments>http://anthonygthomas.com/2010/03/14/display-form-fields-based-on-selection-using-jquery/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 02:59:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[html forms jquery]]></category>

		<guid isPermaLink="false">http://anthonygthomas.com/?p=395</guid>
		<description><![CDATA[This is a simple method of showing and hiding form elements based on the user&#8217;s selection. It&#8217;s based on this article with a couple of very minor changes. (Dare I say, improvements?) I&#8217;m going to assume you&#8217;ve already included the JQuery library so I won&#8217;t cover that here. I want to go straight to the [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple method of showing and hiding form elements based on the user&#8217;s selection. <a href="http://minneapolis.craigslist.org/ram/tag/1642880912.html" onclick="pageTracker._trackPageview('/outgoing/minneapolis.craigslist.org/ram/tag/1642880912.html?referer=');">It&#8217;s based on this article</a> with a couple of very minor changes. (Dare I say, improvements?) I&#8217;m going to assume you&#8217;ve already included the JQuery library so I won&#8217;t cover that here. I want to go straight to the code. (<a href="http://anthonygthomas.com/examples/jquery-display-forms.html">View the example page here</a>.)<span id="more-395"></span></p>
<p>First, the form:</p>
<pre class="brush: xml; title: ; notranslate">&lt;form id=&quot;ExampleForm&quot; method=&quot;post&quot; action=&quot;#&quot;&gt;
	&lt;fieldset&gt;
		&lt;legend&gt;Questionnaire&lt;/legend&gt;
		&lt;div class=&quot;input select&quot;&gt;
			&lt;label for=&quot;select1&quot;&gt;Choose 1 to make the next select list appear.&lt;/label&gt;
			&lt;select name=&quot;select1&quot; id=&quot;select1&quot;&gt;
				&lt;option value=&quot;&quot;&gt;(choose one)&lt;/option&gt;
				&lt;option value=&quot;0&quot;&gt;0&lt;/option&gt;
				&lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
				&lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
				&lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
				&lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;
				&lt;option value=&quot;5&quot;&gt;5&lt;/option&gt;
				&lt;option value=&quot;6&quot;&gt;6&lt;/option&gt;
				&lt;option value=&quot;7&quot;&gt;7&lt;/option&gt;
			&lt;/select&gt;
		&lt;/div&gt;
		&lt;div class=&quot;hide&quot; id=&quot;hide1&quot;&gt;&lt;!-- this select box will be hidden at first --&gt;
			&lt;div class=&quot;input select&quot;&gt;
				&lt;label for=&quot;select2&quot;&gt;Select &quot;Yes&quot; to make the next option appear.&lt;/label&gt;
				&lt;select name=&quot;select2&quot; id=&quot;select2&quot;&gt;
					&lt;option value=&quot;&quot;&gt;(choose one)&lt;/option&gt;
					&lt;option value=&quot;1&quot;&gt;Yes&lt;/option&gt;
					&lt;option value=&quot;0&quot;&gt;No&lt;/option&gt;
					&lt;option value=&quot;3&quot;&gt;Don&amp;#039;t Know&lt;/option&gt;
				&lt;/select&gt;
			&lt;/div&gt;
		&lt;/div&gt;
		&lt;div class=&quot;hide&quot; id=&quot;hide2&quot;&gt; &lt;!-- this one will also be hidden at first. --&gt;
			&lt;div class=&quot;input select&quot;&gt;
				&lt;label for=&quot;select3&quot;&gt;This is the last question.&lt;/label&gt;
				&lt;select name=&quot;select3&quot; id=&quot;select3&quot;&gt;
					&lt;option value=&quot;&quot;&gt;(choose one)&lt;/option&gt;
					&lt;option value=&quot;0&quot;&gt;0&lt;/option&gt;
					&lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
					&lt;option value=&quot;2&quot;&gt;2 to 5&lt;/option&gt;
					&lt;option value=&quot;3&quot;&gt;6 to 10&lt;/option&gt;
					&lt;option value=&quot;4&quot;&gt;&amp;gt;10&lt;/option&gt;
				&lt;/select&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/fieldset&gt;
	&lt;div class=&quot;submit&quot;&gt;
		&lt;input type=&quot;submit&quot; value=&quot;Save Answers&quot; /&gt;
	&lt;/div&gt;
&lt;/form&gt;</pre>
<p>Now make sure your &#8220;hide&#8221; css class is hidden:</p>
<pre class="brush: css; title: ; notranslate">.hide
{

	display:none;

}</pre>
<p>Now with the wonderful elegance of JQuery, we&#8217;ll tell the browser to display the hidden divs based on the user&#8217;s selection:</p>
<pre class="brush: jscript; title: ; notranslate">$(document).ready(function(){
	$(&quot;#select1&quot;).change(function(){

		if ($(this).val() == &quot;1&quot; ) {

			$(&quot;#hide1&quot;).slideDown(&quot;fast&quot;); //Slide Down Effect

		} else {

			$(&quot;#hide1&quot;).slideUp(&quot;fast&quot;);	//Slide Up Effect

		}
	});

	$(&quot;#select2&quot;).change(function(){

		if ($(this).val() == &quot;1&quot; ) {

			$(&quot;#hide2&quot;).slideDown(&quot;fast&quot;); //Slide Down Effect

		} else {

			$(&quot;#hide2&quot;).slideUp(&quot;fast&quot;);	//Slide Up Effect

		}
	});
});</pre>
<p>Let&#8217;s look at what&#8217;s happening here. First, we have a form with select boxes with ids &#8220;select1&#8243;, &#8220;select2&#8243; and &#8220;select3&#8243;. The last two are hidden by enclosing them in a div with our &#8220;hide&#8221; class. (Incidentally, Blueprint provides a &#8220;hide&#8221; class for you if you use that framework.)</p>
<p>Next we tell JQuery that as soon as &#8220;select1&#8243; is changed, we need to evaluate the new value. In this case, if the new value is &#8220;1&#8243;, we want to display the div with the id &#8220;hide1&#8243; <a href="http://api.jquery.com/slideDown/" onclick="pageTracker._trackPageview('/outgoing/api.jquery.com/slideDown/?referer=');">using JQuery&#8217;s slideDown effect</a>. We do this with <a href="http://api.jquery.com/change/" onclick="pageTracker._trackPageview('/outgoing/api.jquery.com/change/?referer=');">JQuery&#8217;s change event</a>.</p>
<pre class="brush: jscript; title: ; notranslate">$(&quot;#select1&quot;).change(function(){ // when #select1 changes

		if ($(this).val() == &quot;1&quot; ) { // see if the new value is &quot;1&quot;

			$(&quot;.hide1&quot;).slideDown(&quot;fast&quot;); // if it is &quot;1&quot;, display the .hide1 div with the slideDown effect

		} else {

			$(&quot;.hide1&quot;).slideUp(&quot;fast&quot;);	//otherwise, hide it with the slideUp effect

		}
	});</pre>
<p>Once &#8220;select2&#8243; is displayed, we can evaluate it to see if we need to display &#8220;select3&#8243; which is in a div with a &#8220;hide&#8221; class and &#8220;hide2&#8243; id.</p>
<pre class="brush: jscript; title: ; notranslate">$(&quot;#select2&quot;).change(function(){ // once select2 is changed

		if ($(this).val() == &quot;1&quot; ) { // see if the new value is &quot;1&quot;

			$(&quot;.hide2&quot;).slideDown(&quot;fast&quot;); // if it is, display the hide2 div using slideDown

		} else {

			$(&quot;.hide2&quot;).slideUp(&quot;fast&quot;);	// otherwise hide it using slideUp

		}
	});</pre>
<p>Remarkably simple really. <a href="http://anthonygthomas.com/examples/jquery-display-forms.html">Review the working example and code here</a>.</p>
<p><strong>Update (March 15, 2010)</strong></p>
<p>You also could show/hide a single form element by id rather than enclose them in a div. I enclosed them in a div 1.) so I could hide the label and 2.) because in my application where I use this solution, I&#8217;m actually hiding a whole group of fields.</p>
<h3>Resources</h3>
<ul>
<li><a href="http://api.jquery.com/change/" onclick="pageTracker._trackPageview('/outgoing/api.jquery.com/change/?referer=');">JQuery change() Event</a></li>
<li><a href="http://api.jquery.com/val/" onclick="pageTracker._trackPageview('/outgoing/api.jquery.com/val/?referer=');">JQuery val()</a></li>
<li><a href="http://api.jquery.com/slideDown/" onclick="pageTracker._trackPageview('/outgoing/api.jquery.com/slideDown/?referer=');">JQuery slideDown Effect</a></li>
<li><a href="http://api.jquery.com/slideUp/" onclick="pageTracker._trackPageview('/outgoing/api.jquery.com/slideUp/?referer=');">JQuery slideUp Effect</a></li>
</ul>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://anthonygthomas.com/2010/03/14/display-form-fields-based-on-selection-using-jquery/" rel="bookmark" title="March 14, 2010">Display Form Fields Based on Selection Using JQuery</a></li>
<li><a href="http://anthonygthomas.com/2010/02/14/why-use-blueprint-and-the-960-grid-system-in-the-baseline-theme/" rel="bookmark" title="February 14, 2010">Why Use Blueprint and the 960 Grid System in the Baseline Theme?</a></li>
<li><a href="http://anthonygthomas.com/2009/07/22/simple-security-in-cakephp/" rel="bookmark" title="July 22, 2009">Simple Security in CakePHP</a></li>
<li><a href="http://anthonygthomas.com/2011/04/18/css-scaffolding/" rel="bookmark" title="April 18, 2011">CSS Scaffolding</a></li>
<li><a href="http://anthonygthomas.com/2011/05/17/new-theme/" rel="bookmark" title="May 17, 2011">New Theme</a></li>
</ul>
<p><!-- Similar Posts took 4.585 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://anthonygthomas.com/2010/03/14/display-form-fields-based-on-selection-using-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

