jcadima
3/24/2017 - 10:32 PM

Add required attribute on radio button click

Add required attribute on radio button click

http://stackoverflow.com/questions/25340186/add-required-attribute-when-radio-is-checked


Clicking on the Partial radio button will trigger a required field 
in Number of Pallets, and remove the required attribute when the Partial radio button
is unchecked


<div class="row">
  <div class="form-group col-md-12 col-xs-12">
      <label class="col-md-4">Truckload Type *</label>
     
      <div class="form-check col-md-2">
        <label class="form-check-label">
          <input type="radio" class="form-check-input"  name="truckload_type" value="Full" checked>
          Full
        </label>
      </div>
      <div class="form-check col-md-2">
      <label class="form-check-label">
          <input type="radio" class="form-check-input"  name="truckload_type" id="partialtruckload" value="Partial">
          Partial
        </label>
      </div>
  
  </div><!--formgroup-->

</div><!--row-->






<div class="row">

  <!-- Text input-->
  <div class="form-group col-md-12 col-xs-12">
    <label class="col-md-4 control-label" for="lastName">Number of Pallets</label>  
    <div class="col-md-8">
    <input id="pallets" name="pallets" type="text" placeholder="100" class="form-control input-md">
      
  </div>
  </div><!--formgroup-->


</div><!--row-->




<script>
$( document ).ready(function() {

  $('input[name="truckload_type"]').change(function () {
      if($("#partialtruckload").is(':checked')) {
          $('#pallets').attr('required', true);
      } else {
          $('#pallets').removeAttr('required');
      }
  });

});	

</script>