Skip to content Skip to sidebar Skip to footer

Show/hide Form Contents Based On Radio Selections

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.

Solution 1:

I 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.net/cyTLm/


Solution 2:

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.net/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;
}

Post a Comment for "Show/hide Form Contents Based On Radio Selections"