title: How to toggle required field class with jQuery Validation plugin?
body: <p>My application has a dynamic form that uses jQuery to toggle the display of follow up questions, and make certain questions required using the jQuery Validation plugin.  My problem is that when the form loads, with previously answered questions, the correct classes are not displaying.</p>
  
  <p>The "follow up" questions display a textarea if 'yes' is answered.  If 'yes' is selected, and the textarea is displayed, the textarea should be a required field (class="required").  If 'no' is selected, and the textarea is hidden, the textarea should not be required.</p>
  
  <p>If you look at a working example, <a href="http://jsfiddle.net/GKATm/" rel="nofollow">http://jsfiddle.net/GKATm/</a>, and review the source code or use Firebug, the hidden textarea is set as required:</p>
  
  <pre><code>&lt;span class="details" style="display: none;"&gt;
       &lt;textarea id="followup_1_details" maxlength="1000" class="required"&gt;&lt;/textarea&gt;
  &lt;/span&gt;
  </code></pre>
  
  <p>Any ideas on what I am doing wrong.  Everything works when the form loads blank.  But my application allows users to return to their saved answers, and when they do this the Validation plugin is flagging this as invalid, because a required field has not been answered.</p>
  
  <p>Please help! </p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div&gt;    
      &lt;label&gt;Question #1&lt;/label&gt;
      &lt;span class="options"&gt;
          No  &lt;input type="radio" class="toggle_group required" value="0" id="restrictions_no" name="restrictions"&gt;
          Yes &lt;input type="radio" class="toggle_group required" value="1" id="restrictions_yes" name="restrictions" checked="checked"&gt;
      &lt;/span&gt;
  &lt;/div&gt;
  
  &lt;div class="details_group"&gt;
      &lt;div&gt;
          &lt;label&gt;Follow Up #1&lt;/label&gt;
          &lt;span&gt;
              No  &lt;input type="radio" class="toggle" value="0" id="followup_1_no" name="followup_1" checked="checked"&gt;
              Yes &lt;input type="radio" class="toggle" value="1" id="followup_1_yes" name="followup_1"&gt;
          &lt;/span&gt;            
          &lt;span class="details"&gt;
              &lt;textarea maxlength="1000" id="followup_1_details"&gt;&lt;/textarea&gt;
          &lt;/span&gt;
      &lt;/div&gt;
  
      &lt;div&gt;
          &lt;label&gt;Follow Up #2&lt;/label&gt;
          &lt;span&gt;
              No  &lt;input type="radio" class="toggle" value="0" id="followup_2_no" name="followup_2"&gt;
              Yes &lt;input type="radio" class="toggle" value="1" id="followup_2_yes" name="followup_2" checked="checked"&gt;
          &lt;/span&gt;            
          &lt;span class="details"&gt;
              &lt;textarea maxlength="1000" id="followup_2_details"&gt;&lt;/textarea&gt;
          &lt;/span&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>$('.toggle').on('change', function() {
  
      var showOrHide = false;
  
      $(this).siblings('input[type=radio]').andSelf().each(function() {
          if ($(this).val() == 1 &amp;&amp; $(this).prop("checked")) showOrHide = true;
      })
  
      $(this).parent().next('.details').toggle(showOrHide);
      $(this).parent().next('.details').find('textarea').addClass('required');
  
      if (showOrHide == false) {
          $(this).parent().next('.details').find('textarea').val('');
          $(this).parent().next('.details').find('textarea').removeClass('required');
      }
  
  }).change()
  
  
  $('.toggle_group').on('change', function() {
  
      var showOrHide = false;
  
      $(this).siblings('input[type=radio]').andSelf().each(function() {
          if ($(this).val() == 1 &amp;&amp; $(this).prop("checked")) showOrHide = true;
      })
  
      $(this).parent().parent().next('.details_group').toggle(showOrHide)
      $(this).parent().parent().next('.details_group').find('input:radio').addClass('required');
      $(this).parent().parent().next('.details_group').find('textarea').addClass('required');
  
      if (showOrHide == false) {
          $(this).parent().parent().next('.details_group').find('input:radio').removeAttr('checked')
          $(this).parent().parent().next('.details_group').find('input:radio').removeClass('required');
          $(this).parent().parent().next('.details_group').find('textarea').val('');
          $(this).parent().parent().next('.details_group').find('textarea').removeClass('required');
  
      }
  
  }).change()  
  </code></pre>