title: Keep checkbox values on content update
body: <p>I've been struggling with this issue for a while now. Maybe you can help.</p>
  
  <p>I have a table with a checkbox at the beginning of each row. I defined a function which reloads the table at regular intervals. It uses jQuery's load() function on a JSP which generates the new table.</p>
  
  <p>The problem is that I need to preserve the checkbox values until the user makes up his mind on which items to select. Currently, their values are lost between updates.</p>
  
  <p>The current code I use that tries to fix it is:</p>
  
  <pre><code> refreshId = setInterval(function()
   {
          var allTicks = new Array();
          $('#myTable input:checked').each(function() {
              allTicks.push($(this).attr('id'));
          });
          $('#myTable').load('/get-table.jsp', null,
                  function (responseText,textStatus, req ){
              $('#my-table').tablesorter();
              //alert(allTicks + ' length ' + allTicks.length);
              for (i = 0 ; i &lt; allTicks.length; i++ )
                  $("#my-table input#" + allTicks[i]).attr('checked', true);
  
          });
  }, $refreshInterval); 
  </code></pre>
  
  <p>The id of each checkbox is the same as the table entry next to it.</p>
  
  <p>My idea was to store all the checked checkboxes' ids into an array before the update and to change their values after the update is done, as most of the entries will be preserved, and the ones that are new won't really matter.</p>
  
  <p>'#myTable' is the div in which the table is loaded and '#my-table' is the id of the table which is generated. The checkbox inputs are generated along with the new table and with the same ids as before.</p>
  
  <p>The weird thing is that applying tablesorter to the newly generated table works, but getting the elements with the stored ids doesn't.</p>
  
  <p>Any solutions?</p>
  
  <p>P.S: I know that this approach to table generation isn't really the best, but my JS skills were limited back then. I'd like to keep this solution for now and fix the problem.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Applied the syntax suggested by Didier G. and added some extra test blocks that check the status before and after the checkbox ticking.</p>
  
  <p>Looks like this now:</p>
  
  <pre><code>refreshId = setInterval(function()
      {
          var allTicks = []
          var $myTable = $('#my-table');
          allTicks = $myTable.find('input:checked').map(function() { return this.id; });
  
          $('#myTable').load('/get-table.jsp', null,
          function (responseText,textStatus, req ){
              $myTable = $('#my-table');
              $('#my-table').tablesorter();
              var msg = 'Before: \n';
              $myTable.find('input').each(function(){
                  msg = msg + this.id + " " + $(this).prop('checked') + '\n';
              });
              //alert(msg);
              //alert(allTicks + ' length ' + allTicks.length);
              for (i = 0 ; i &lt; allTicks.length; i++ ){
                   $myTable.find('#' + allTicks[i]).prop('checked', true);
              }
              msg = 'After: '
              $myTable.find('input').each(function(){
                  msg = msg + this.id + " " + $(this).prop('checked') + '\n';
              });
              //alert(msg);
          });
      }, $refreshInterval);
  </code></pre>
  
  <p>If I uncomment the alert lines, and check 2 checkboxes, on the next update I get (for 3 row table):</p>
  
  <pre><code>Before: host2 false
  host3 false
  host4 false
  
  object [Object] length 2
  
  After: host2 false
  host3 false
  host4 false
  </code></pre>
  
  <p>Also did a previous check on the contents of the array and it has all the correct entries.</p>
  
  <p>Can the DOM change or working with an entirely new table instance be a cause of this?</p>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>Here's a sample of the table generated by the JSP (edited for confidentiality purposes):</p>
  
  <pre><code>&lt;table id="my-table" class="tablesorter"&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Full Name&lt;/th&gt; 
          &lt;th&gt;IP Address&lt;/th&gt; 
          &lt;th&gt;Role&lt;/th&gt; 
          &lt;th&gt;Job Slots&lt;/th&gt; 
          &lt;th&gt;Status&lt;/th&gt;
          &lt;th&gt;Management&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;head&lt;/td&gt;
          &lt;td&gt;10.20.1.14&lt;/td&gt;
          &lt;td&gt;H&lt;/td&gt;
          &lt;td&gt;4&lt;/td&gt;
          &lt;td&gt;ON&lt;/td&gt;
          &lt;td&gt;Permanent&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;input type="checkbox" id="host2" name="host2"/&gt;
              host2
          &lt;/td&gt;
          &lt;td&gt;10.20.1.7&lt;/td&gt;
          &lt;td&gt;C&lt;/td&gt;
          &lt;td&gt;4&lt;/td&gt;
          &lt;td&gt;BSTART&lt;/td&gt;
          &lt;td&gt;Dynamic&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;input type="checkbox" id="host3" name="host3"/&gt;
          host3&lt;/td&gt;
          &lt;td&gt;10.20.1.9&lt;/td&gt;
          &lt;td&gt;C&lt;/td&gt;
          &lt;td&gt;4&lt;/td&gt;
          &lt;td&gt;BSTART&lt;/td&gt;
          &lt;td&gt;Dynamic&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;input type="checkbox" id="host4" name="host4"/&gt;
          host4&lt;/td&gt;
          &lt;td&gt;10.20.1.11&lt;/td&gt;
          &lt;td&gt;C&lt;/td&gt;
          &lt;td&gt;4&lt;/td&gt;
          &lt;td&gt;BSTART&lt;/td&gt;
          &lt;td&gt;Dynamic&lt;/td&gt;
      &lt;/tr&gt;
  
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Note that the id and name of the checkbox coincide with the host name. Also note that the first td does not have a checkbox. That's the expected behavior.</p>