title: Rearranging list elements in jQuery
body: <p>I am making a <code>ul li</code> element function like a <code>select</code> input from which user can select a color for the box.</p>
  
  <p>Here's the code: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {
  
    $('.color-picker ul.color-list li.active .text').after('&lt;span class="pull-right glyphicon glyphicon-menu-down"&gt;&lt;/span&gt;');
  
    $('.color-picker ul.color-list li').click(function() {
  
  
      if ($(this).hasClass('active')) {
  
        console.log(2);
        $('.color-picker ul.color-list li').show();
      } else {
        console.log(1);
  
        var color = $(this).find('.box').css('backgroundColor');
        $('.color-picker .color-box').css({
          'background-color': color
        });
  
        $('.color-picker ul.color-list li.active').removeClass('active');
        $('.color-picker ul.color-list li').hide();
  
        $(this).addClass('active');
        $('.color-picker ul.color-list li.active').show();
        $('.color-picker ul.color-list li .glyphicon').remove();
        $('.color-picker ul.color-list li.active .text').after('&lt;span class="pull-right glyphicon glyphicon-menu-down"&gt;&lt;/span&gt;');
  
      }
  
    });
  
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.color-picker {
    display: flex;
    margin: 20px;
    border: 1px solid #d9d9d9;
    padding: 10px;
    width: 350px;
  }
  .color-picker .color-box {
    display: inline-block;
    background-color: black;
    height: 50px;
    width: 50px;
  }
  .color-picker ul.color-list {
    display: inline-block;
    margin: 0;
    position: absolute;
  }
  .color-picker ul.color-list li {
    list-style: none;
    border: 1px solid #d9d9d9;
    padding: 10px;
    cursor: pointer;
    width: 200px;
    background-color: white;
  }
  .color-picker ul.color-list li.active {
    display: block;
  }
  .color-picker ul.color-list li {
    display: none;
  }
  .color-picker ul.color-list li .box {
    content: "";
    display: inline-block;
    height: 10px;
    width: 30px;
    margin-right: 10px;
  }
  .color-picker ul.color-list li.red .box {
    background-color: #c71212;
  }
  .color-picker ul.color-list li.blue .box {
    background-color: #0d89d1;
  }
  .color-picker ul.color-list li.green .box {
    background-color: #18c771;
  }
  .color-picker .as-select .select-default {
    border: 1px solid #d9d9d9;
    padding: 10px 20px;
    width: 200px;
    margin-left: 40px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div class="color-picker"&gt;
  
    &lt;div class="color-box"&gt;
    &lt;/div&gt;
  
    &lt;div class="as-select"&gt;
      &lt;ul class="color-list"&gt;
        &lt;li class="red active"&gt;
          &lt;span class="box"&gt;&lt;/span&gt;
          &lt;span class="text"&gt;Red&lt;/span&gt;
        &lt;/li&gt;
        &lt;li class="blue"&gt;
          &lt;span class="box"&gt;&lt;/span&gt;
          &lt;span class="text"&gt;Blue&lt;/span&gt;
        &lt;/li&gt;
        &lt;li class="green"&gt;
          &lt;span class="box"&gt;&lt;/span&gt;
          &lt;span class="text"&gt;Green&lt;/span&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://jsfiddle.net/jr47ychh/6/" rel="nofollow">jsFiddle</a></p>
  
  <p>As you can see the active element remains at the same place.</p>
  
  <p>(Select Blue color and click drop down again)</p>
  
  <p>Is their a way I can make the selected element the first child of the list?</p>