title: How to check/uncheck dynamically in a JSP page
body: <p>I am working with JSP and Servlet. Below is my JSP form which has drop down list and checkbox list. As soon as I click <code>Access</code> button, my drop down list gets populated dynamically with all the email id's and also my checkbox list gets populated dynamically as well with all the item id's.</p>
  
  <pre><code>    &lt;p id="one"&gt;
          &lt;input type="button" class="btn-primary" id="per" value="Access"/&gt;
      &lt;/p&gt;
  
      &lt;form id="form_permission" method="post" action="/test/"&gt;
          &lt;h4&gt;Welcome!&lt;/h4&gt;
  
          &lt;p&gt; Users
              &lt;select id="yes"&gt;&lt;/select&gt;
          &lt;/p&gt;
  
          &lt;div id="id1" class="container"&gt;&lt;/div&gt;
          &lt;input type="submit" value="Save"&gt;
          &lt;input type='hidden' name='action' value='per'&gt;
  
      &lt;/form&gt;
  </code></pre>
  
  <p>Below is my jquery for that - </p>
  
  <pre><code>    $(document).ready(function () {
          $('.btn-primary').click(function () {
              $('.btn-primary').removeClass('currentButton')
              $(this).addClass('currentButton')
              $('form').hide()
              $("#form_" + $(this).attr('id')).show()
              $.get('/emailservlet', function (responseJson) {
                  var $select = $('#yes');
                  $select.find('option').remove();
                  $.each(responseJson, function (key, value) {
                      $('&lt;option&gt;').val(key).text(value).appendTo($select);
                  });
              });
              $.get('/itemsservlet', function (responseJson) {
                  var $input = $('#id1');
                  var $type = 'checkbox';
                  $.each(responseJson, function (key, value) {
                      var append = $('&lt;input type="' + $type + '"&gt;' + value + '&lt;/input&gt;').attr('id', key).val(key);
                      $($input).append('&lt;br/&gt;').append(append);
                  });
              });
          });
      });
  </code></pre>
  
  <p>Each email id is responsible for multiple items. Meaning <code>abc@gmail.com</code> is responsible for item whose id's are for exaample <code>1000</code>, <code>1001</code>, <code>1002</code>.
  In general, as soon as I select any email id, I will make an ajax call to the database to get the list of item id's this email is responsible for.</p>
  
  <p>Now what I need to do is, As soon as I select <code>abc@gmail.com</code> from the drop down list, I need to check those ITEM's in the checkbox list whose id's are <code>1000</code>, <code>1001</code> and <code>1002</code></p>
  
  <p>For example, getting items from a database such as, <code>select items from table1 where email ='abc@gmail.com'</code> will give all the item id's whose email was <code>abc@gmail.com</code>, Now in my jsp page when I select any email, it should check the items based on that email if from a database?</p>
  
  <p>Is this possible to do?</p>
  
  <p><strong>Update:-</strong></p>
  
  <p>This is the way I am sending data - </p>
  
  <pre><code>List&lt;String&gt; permission_given = new ArrayList&lt;String&gt;();
  String permissions = null;
  while (rs.next()) {
      permissions = rs.getString("permissions");
      // here permissions has id's like this 1000,1001,1002
  }
  
  permission_given = Arrays.asList(permissions.split(","));
  String json = new Gson().toJson(permission_given);
  response.setContentType("application/json");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write(json);
  </code></pre>