Skip to content

Instantly share code, notes, and snippets.

@zechtz
Created September 10, 2015 16:52
Show Gist options
  • Save zechtz/bb938550a0f671af6786 to your computer and use it in GitHub Desktop.
Save zechtz/bb938550a0f671af6786 to your computer and use it in GitHub Desktop.
Control Show Hide Form elements depending on selected value
<form accept-charset="UTF-8" action="#" class="search_aside" id="advanced_search" method="post">
<div class="row">
<select id="practice_area" name="advanced_search[law_area_id]">
<option value="">Select Area of Practice</option>
<option value="14">Admiralty &amp; Maritime Law</option>
<option value="24">Banking and Finance Law</option>
</select>
</div>
<div class="row">
<select id="advanced_search_country_id" name="advanced_search[country_id]"><option value="">Select Country</option>
<option value="1">Kenya</option>
<option value="2">Tanzania</option>
</select>
</div>
<div class="row">
<select id="advanced_search_county_id" name="advanced_search[county_id]">
<option value="">Select County</option>
<optgroup label="Kenya">
<option value="1">Nairobi</option>
<option value="2">Mombasa</option>
</optgroup>
<optgroup label="Tanzania">
<option value="53">Dar es salaam</option>
<option value="54">Zanzibar</option>
</optgroup>
<optgroup label="Rwanda">
<option value="63">Kigali</option>
<option value="64">Butare</option>
</optgroup>
<optgroup label="Uganda">
<option value="73">Kampala</option>
<option value="74">Gulu</option>
</optgroup>
<optgroup label="Burundi">
<option value="83">Bujumbura</option>
<option value="84">Muyinga</option>
</optgroup>
</select>
</div>
<div class="row">
<input class="button" id="button" type="submit" value="Search">
</div>
</form>
jQuery(function() {
var counties;
$('#law_firm_county_id').parent().hide();
counties = $('#law_firm_county_id').html();
return $('#law_firm_country_id').change(function() {
var country, options;
country = $('#law_firm_country_id :selected').text();
options = $(counties).filter("optgroup[label='" + country + "']").html();
if (options) {
$('#law_firm_county_id').html(options);
return $('#law_firm_county_id').parent().show();
} else {
$('#law_firm_county_id').empty();
return $('#law_firm_county_id').parent().hide();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment