title: jQuery Select Swap Option on Change
body: <p>I have a page that allows a user to select the order of how fields are displayed. The drop downs are dynamically created and you only have the number of options based on the number of fields there are. </p>
  
  <p>For example, if there are 5 fields, you have options 1-5 to sort your fields by.</p>
  
  <p>What I am trying to accomplish is when you select a number from the dropdown, it "Swaps" that number with whatever one previously held that spot.</p>
  
  <p>If I changed Record 4 to 3, those two drop downs would now be swapped... if that makes sense.</p>
  
  <p>In the example below, change one of the numeric dropdowns to another choice. The original choice that held that value is updated but the actual one you are changing doesn't get the new value.</p>
  
  <p>JS Fiddle: <a href="https://jsfiddle.net/y7g155mh/4/" rel="nofollow">https://jsfiddle.net/y7g155mh/4/</a></p>
  
  <pre><code>&lt;table name="selectedFields" class="table table-condensed selectedFields"&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th class="small span1"&gt;
          &lt;input type="checkbox" id="checkAllSelected"&gt;
        &lt;/th&gt;
        &lt;th class="small"&gt;Field Name&lt;/th&gt;
        &lt;th class="small"&gt;Sort Order&lt;/th&gt;
        &lt;th class="small"&gt;Sort Type&lt;/th&gt;
        &lt;th class="small"&gt;Display Order&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody name="selectedRows"&gt;
      &lt;tr data-fid="5"&gt;
        &lt;td class="small"&gt;&lt;/td&gt;
        &lt;td class="small"&gt;Request ID&lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="1" data-type="sortOrder" class="span1" name="sortOrder"&gt;
            &lt;option selected="selected" value="1"&gt;1&lt;/option&gt;
            &lt;option value="2"&gt;2&lt;/option&gt;
            &lt;option value="3"&gt;3&lt;/option&gt;
            &lt;option value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select class="span1" name="sortType"&gt;
            &lt;option value="-"&gt;-&lt;/option&gt;
            &lt;option selected="selected" value="ASC"&gt;ASC&lt;/option&gt;
            &lt;option value="DESC"&gt;DESC&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="1" data-type="displayOrder" class="span1" name="displayOrder"&gt;
            &lt;option selected="selected" value="1"&gt;1&lt;/option&gt;
            &lt;option value="2"&gt;2&lt;/option&gt;
            &lt;option value="3"&gt;3&lt;/option&gt;
            &lt;option value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr data-fid="16"&gt;
        &lt;td class="small"&gt;&lt;/td&gt;
        &lt;td class="small"&gt;Task ID&lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="2" data-type="sortOrder" class="span1" name="sortOrder"&gt;
            &lt;option value="1"&gt;1&lt;/option&gt;
            &lt;option selected="selected" value="2"&gt;2&lt;/option&gt;
            &lt;option value="3"&gt;3&lt;/option&gt;
            &lt;option value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select class="span1" name="sortType"&gt;
            &lt;option value="-"&gt;-&lt;/option&gt;
            &lt;option selected="selected" value="ASC"&gt;ASC&lt;/option&gt;
            &lt;option value="DESC"&gt;DESC&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="2" data-type="displayOrder" class="span1" name="displayOrder"&gt;
            &lt;option value="1"&gt;1&lt;/option&gt;
            &lt;option selected="selected" value="2"&gt;2&lt;/option&gt;
            &lt;option value="3"&gt;3&lt;/option&gt;
            &lt;option value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr data-fid="9"&gt;
        &lt;td class="small"&gt;&lt;/td&gt;
        &lt;td class="small"&gt;Task Start Date&lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="3" data-type="sortOrder" class="span1" name="sortOrder"&gt;
            &lt;option value="1"&gt;1&lt;/option&gt;
            &lt;option value="2"&gt;2&lt;/option&gt;
            &lt;option selected="selected" value="3"&gt;3&lt;/option&gt;
            &lt;option value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select class="span1" name="sortType"&gt;
            &lt;option value="-"&gt;-&lt;/option&gt;
            &lt;option selected="selected" value="ASC"&gt;ASC&lt;/option&gt;
            &lt;option value="DESC"&gt;DESC&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="3" data-type="displayOrder" class="span1" name="displayOrder"&gt;
            &lt;option value="1"&gt;1&lt;/option&gt;
            &lt;option value="2"&gt;2&lt;/option&gt;
            &lt;option selected="selected" value="3"&gt;3&lt;/option&gt;
            &lt;option value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr data-fid="17"&gt;
        &lt;td class="small"&gt;&lt;/td&gt;
        &lt;td class="small"&gt;Task Completion Date&lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="4" data-type="sortOrder" class="span1" name="sortOrder"&gt;
            &lt;option value="1"&gt;1&lt;/option&gt;
            &lt;option value="2"&gt;2&lt;/option&gt;
            &lt;option value="3"&gt;3&lt;/option&gt;
            &lt;option selected="selected" value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select class="span1" name="sortType"&gt;
            &lt;option value="-"&gt;-&lt;/option&gt;
            &lt;option selected="selected" value="ASC"&gt;ASC&lt;/option&gt;
            &lt;option value="DESC"&gt;DESC&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
        &lt;td class="small"&gt;
          &lt;select data-current="4" data-type="displayOrder" class="span1" name="displayOrder"&gt;
            &lt;option value="1"&gt;1&lt;/option&gt;
            &lt;option value="2"&gt;2&lt;/option&gt;
            &lt;option value="3"&gt;3&lt;/option&gt;
            &lt;option selected="selected" value="4"&gt;4&lt;/option&gt;
          &lt;/select&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>$('body').on('change', 'select', function() {
    saveOrder($(this).val(), $(this).data('type'), $(this).data('current'));
  });
  
  // Update the field order numbers and save the data
  function saveOrder(order, type, current) {
    // First thing we need to do is swap the selected value with the one we are changing it for.
    $('[name=' + type + ']').find('option[value="' + order + '"]:selected').parent().val(current);
    return false;
  
    // Do something here with storing data
  }
  </code></pre>