Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My plugin version - <strong><a href="http://jsbin.com/ihuqo" rel="noreferrer">Working Demo</a></strong></p> <p>Takes an array and optional id prefix and reorders elements whose ids correspond to the order of (id prefix) + values inside the array. Any values in the array that don't have an element with the corresponding id will be ignored, and any child elements that do not have an id within the array will be removed.</p> <pre><code>(function($) { $.fn.reOrder = function(array, prefix) { return this.each(function() { prefix = prefix || ""; if (array) { for(var i=0; i &lt; array.length; i++) array[i] = $('#' + prefix + array[i]); $(this).empty(); for(var i=0; i &lt; array.length; i++) $(this).append(array[i]); } }); } })(jQuery); </code></pre> <p>Code from the demo</p> <p><strong>jQuery</strong></p> <pre><code> $(function() { $('#go').click(function() { var order = $('#order').val() == ""? null: $('#order').val().split(","); $('#container').reOrder(order, 'someid'); }); }); (function($) { $.fn.reOrder = function(array, prefix) { return this.each(function() { prefix = prefix || ""; if (array) { for(var i=0; i &lt; array.length; i++) array[i] = $('#' + prefix + array[i]); $(this).empty(); for(var i=0; i &lt; array.length; i++) $(this).append(array[i]); } }); } })(jQuery); </code></pre> <p><strong>HTML</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; &lt;title&gt;reOrder Demo&lt;/title&gt; &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt; &lt;style type="text/css" media="screen"&gt; body { background-color: #fff; font: 16px Helvetica, Arial; color: #000; } div.style { width: 200px; height: 100px; border: 1px solid #000000; margin: 5px; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt; &lt;div id="someid1" class="style" style="background-color:green;"&gt;div1&lt;/div&gt; &lt;div id="someid2" class="style" style="background-color:blue;"&gt;div2&lt;/div&gt; &lt;div id="someid3" class="style" style="background-color:red;"&gt;div3&lt;/div&gt; &lt;div id="someid4" class="style" style="background-color:purple;"&gt;div4&lt;/div&gt; &lt;/div&gt; &lt;p&gt;Pass in a comma separated list of numbers 1-4 to reorder divs&lt;/p&gt; &lt;input id="order" type="text" /&gt; &lt;input id="go" type="button" value="Re-Order" /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload