Im building a form that has conditional fields that need to show based on what checkboxes are selected. I want to know how I can hide other form fields when a checkbox is selected. I also want to be able to show all fields when both checkboxes are selected.
In the markup below, I have added ments where I want to render the html.
<form class="form-horizontal">
<fieldset>
<!-- Multiple Checkboxes -->
<div class="control-group">
<label class="control-label">Multiple Checkboxes</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="checkboxes" value="Load">
Load
</label>
<label class="checkbox">
<input type="checkbox" name="checkboxes" value="Unload">
Unload
</label>
</div>
</div>
<!-- Show when "load" is checked -->
<div class="control-group">
<label class="control-label">Dorms</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>Degraff</option>
</select>
</div>
</div>
<!-- Show when "load" is checked -->
<div class="control-group">
<label class="control-label">Greek House</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>Tri Delt</option>
</select>
</div>
</div>
<!-- Show when "unload" is checked -->
<div class="control-group">
<label class="control-label">How many hops?</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
</select>
</div>
</div>
<!-- Show when "unload" is checked -->
<div class="control-group">
<label class="control-label">How many hours?</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>One</option>
<option>Two</option>
</select>
</div>
</div>
</fieldset>
</form>
Im building a form that has conditional fields that need to show based on what checkboxes are selected. I want to know how I can hide other form fields when a checkbox is selected. I also want to be able to show all fields when both checkboxes are selected.
In the markup below, I have added ments where I want to render the html.
<form class="form-horizontal">
<fieldset>
<!-- Multiple Checkboxes -->
<div class="control-group">
<label class="control-label">Multiple Checkboxes</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="checkboxes" value="Load">
Load
</label>
<label class="checkbox">
<input type="checkbox" name="checkboxes" value="Unload">
Unload
</label>
</div>
</div>
<!-- Show when "load" is checked -->
<div class="control-group">
<label class="control-label">Dorms</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>Degraff</option>
</select>
</div>
</div>
<!-- Show when "load" is checked -->
<div class="control-group">
<label class="control-label">Greek House</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>Tri Delt</option>
</select>
</div>
</div>
<!-- Show when "unload" is checked -->
<div class="control-group">
<label class="control-label">How many hops?</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
</select>
</div>
</div>
<!-- Show when "unload" is checked -->
<div class="control-group">
<label class="control-label">How many hours?</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>One</option>
<option>Two</option>
</select>
</div>
</div>
</fieldset>
</form>
Share
Improve this question
asked May 2, 2013 at 0:30
Mike LittMike Litt
654 silver badges10 bronze badges
3
- To change the DOM you need some client-side language. Use Javascript or Jquery (a javascript lib) for that – Francisco Afonso Commented May 2, 2013 at 0:32
- Im still grasping jquery/javascript. Any examples of this effect I can look at? – Mike Litt Commented May 2, 2013 at 0:34
- I wont give you any code, you need to understand how things work first, im just going to tell you to use jquery .hide() and .show() methods. Try some tutorials before starting a new language from scratch. – Francisco Afonso Commented May 2, 2013 at 0:35
2 Answers
Reset to default 4I added some id
attributes to your elements.
HTML
<form class="form-horizontal">
<fieldset>
<!-- Multiple Checkboxes -->
<div class="control-group">
<label class="control-label">Multiple Checkboxes</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="checkboxes" value="Load">
Load
</label>
<label class="checkbox">
<input type="checkbox" name="checkboxes" value="Unload">
Unload
</label>
</div>
</div>
<!-- Show when "load" is checked -->
<div id="dorms" class="control-group">
<label class="control-label">Dorms</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>Degraff</option>
</select>
</div>
</div>
<!-- Show when "load" is checked -->
<div id="greek" class="control-group">
<label class="control-label">Greek House</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>Tri Delt</option>
</select>
</div>
</div>
<!-- Show when "unload" is checked -->
<div id="hops" class="control-group">
<label class="control-label">How many hops?</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
</select>
</div>
</div>
<!-- Show when "unload" is checked -->
<div id="hours" class="control-group">
<label class="control-label">How many hours?</label>
<div class="controls">
<select id="selectbasic" name="selectbasic" class="input-xlarge">
<option>One</option>
<option>Two</option>
</select>
</div>
</div>
</fieldset>
</form>
jQuery
$("input[value='Load']").click(function(){
if($(this).is(":checked")){
$("#dorms, #greek").show();
}else{
$("#dorms, #greek").hide();
}
});
$("input[value='Unload']").click(function(){
if($(this).is(":checked")){
$("#hops, #hours").show();
}else{
$("#hops, #hours").hide();
}
});
CSS
#greek, #dorms, #hops, #hours{
display:none;
}
Working Example http://jsfiddle/cyTLm/
Probably this is something that may provide you pointers on your need. You can explore more. I have made small changes in your html and using jquery(since you have tagged) and little css.
Check out the fiddle-
.toggle()
http://jsfiddle/kvaYG/
Jquery
$(':checkbox[value=Load]').change(function(){
$('.load').toggle(); //This will get triggered during the checkbox change
//i.e when you check or unckeck the checkbox. You can use element.toggle()
//which will show and hide based on its last appearance whether it was shown or not.
//This is a shortcut to using .show() and .hide()
});
$(':checkbox[value=Unload]').change(function(){
$('.unload').toggle();
});
Css
/*Initially hide all your sections*/
.load
{
display:none;
}
.unload
{
display:none;
}