Show/hide on a prefilled form
<p>I have a form with a question. If answered yes, a set of checkboxes appears; if answered no nothing happens.</p>

<pre><code>&lt;tr&gt;
  &lt;td&gt;Question?&lt;/td&gt;
  &lt;td&gt;
    &lt;label&gt;&lt;input name="selector" type="radio" value="yes" /&gt;Yes&lt;/label&gt;
    &lt;label&gt;&lt;input name="selector" type="radio" value="no" /&gt;No&lt;/label&gt;
    &lt;br /&gt;
    &lt;div id="subset"&gt;
      &lt;label class="disabled"&gt;&lt;input name="select1" type="checkbox" value="select1" /&gt;option1&lt;/label&gt;&lt;br /&gt;
      &lt;label class="disabled"&gt;&lt;input name="select2" type="checkbox" value="select2" /&gt;option2&lt;/label&gt;&lt;br /&gt;
      &lt;label class="disabled"&gt;&lt;input name="select3" type="checkbox" value="select3" /&gt;option3&lt;/label&gt;&lt;br /&gt;
      &lt;label class="disabled"&gt;&lt;input name="select4" type="checkbox" value="select4" /&gt;option4&lt;/label&gt;
    &lt;/div&gt;
  &lt;/td&gt;
&lt;/tr&gt;

&lt;script&gt;
  $(document).ready(function(){
    $('input[name=selector]').click(function(){
      var selector = $(this).val();

      if (selector == 'no') {
        $('#subset label').addClass('disabled');
        $('#subset input').attr('disabled', 'disabled');
        $('#subset input').attr('checked', false);
      } else {
        $('#subset label').removeClass('disabled');
        $('#subset input').removeAttr('disabled');
      }
    });
  });
&lt;/script&gt;
</code></pre>

<p>This code works fine but the form can be pre-filled. If so it has to work the same way: if yes is selected the sset should be visible.</p>

<p>I know I can easily do this with jQuery again in a separate block of code (see below) but I was hoping I could do this by only altering the existing code a little.</p>

<pre><code>var selector = $('input[name=selector]:checked').val();
if (selector == 'no') {
  $('#subset label').addClass('disabled');
  $('#subset input').attr('disabled', 'disabled');
  $('#subset input').attr('checked', false);
} else {
  $('#subset label').removeClass('disabled');
  $('#subset input').removeAttr('disabled');
}
</code></pre>

<p>The pre-fill is done with smarty and looks something like the code below, which I put in all the radio and checkbox tags.</p>

<pre><code>{{if $array.ELEMENT.answer=='VALUE'}} checked{{/if}}
</code></pre>

<p>So the question is, how can I get the extra functionality without adding a lot of code?</p>

<p>Thanks!</p>
