title: jquery remove selected element and append to another
body: <p>I'm trying to re-append a "removed option" to the appropriate select option menu.  </p>
  
  <p>I have three select boxes: "Categories", "Variables", and "Target".  "Categories" is a chained select, so when the user selects an option from it, the "Variables" select box is populated with options specific to the selected categories option.  When the user chooses an option from the "Variables" select box, it's appended to the "Target" select box.  I have a "remove selected" feature so that if a user "removes" a selected element from the "Target" select box, it's removed from "Target" and put back into the pool of "Variables" options.  The problem I'm having is that it appends the option to the "Variables" items indiscriminately.  That is, if the selected category is "Age" the "Variables" options all have a class of "age".  But, if the removed option is an "income" item, it will display in the "Age Variables" option list.  </p>
  
  <p>Here's the HTML markup:</p>
  
  <pre><code>&lt;select multiple="" id="categories" name="categories[]"&gt;
    &lt;option class="category" value="income"&gt;income&lt;/option&gt;
    &lt;option class="category" value="gender"&gt;gender&lt;/option&gt;
    &lt;option class="category" value="age"&gt;age&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;select multiple="multiple" id="variables" name="variables[]"&gt;
    &lt;option class="income" value="10"&gt;$90,000 - $99,999&lt;/option&gt;
    &lt;option class="income" value="11"&gt;$100,000 - $124,999&lt;/option&gt;
    &lt;option class="income" value="12"&gt;$125,000 - $149,999&lt;/option&gt;
    &lt;option class="income" value="13"&gt;Greater than $149,999&lt;/option&gt;
    &lt;option class="gender" value="14"&gt;Male&lt;/option&gt;
    &lt;option class="gender" value="15"&gt;Female&lt;/option&gt;
    &lt;option class="gender" value="16"&gt;Ungendered&lt;/option&gt;
    &lt;option class="age" value="17"&gt;Ages 18-24&lt;/option&gt;
    &lt;option class="age" value="18"&gt;Ages 25-34&lt;/option&gt;
    &lt;option class="age" value="19"&gt;Ages 35-44&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;select height="60" multiple="multiple" id="target" name="target[]"&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>And, here's the js:</p>
  
  <pre><code>/* This determines what options are display in the "Variables" select box */
      var cat = $('#categories');
      var el = $('#variables');
  
      $('#categories option').click(function() {
          var class = $(this).val();
          $('#variables option').each(function() {
              if($(this).hasClass(class)) {
                  $(this).show();
              } else {
                 $(this).hide();
              }
          });
      });
  
  /* This adds the option to the target select box if the user clicks "add" */
      $('#add').click(function() {  
          return !$('#variables option:selected').appendTo('#target');  
      });
  
  /* This is the remove function in its current form, but doesn't append correctly */
      $('#remove').click(function() {
                  $('#target option:selected').each(function() {
                          var class = $(this).attr('class');
  
                          if($('#variables option').hasClass(class)) {
                                  $(this).appendTo('#variables');
                                  sortList('variables');
                          }
                  });
      });
  </code></pre>