Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<title>Sandbox</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
body { background-color: #000; font: 16px Helvetica, Arial; color: #fff; }
</style>
</head>
<body>
<div class="band">
  <table>
    <tr><td>
      <div class="member">
        <ul>
            <li>John</li>
            <li>Lennon</li>
        </ul>
      </div>
    </td></tr>
    <tr><td>
      <div class="member">
        <ul>
            <li>Paul</li>
            <li>McCartney</li>
        </ul>
      </div>
    </td></tr>
    <tr><td>
      <div class="member">
        <ul>
            <li>George</li>
            <li>Harrison</li>
        </ul>
      </div>
    </td></tr>
    <tr><td>
      <div class="member">
        <ul>
            <li>Ringo</li>
            <li>Starr</li>
        </ul>
      </div>
    </td></tr>
  </table>
</div>
<button>Randomize</button>
  
</body>
</html>
 
$('button').click(function() {
  $("div.band").randomize("table tr td", "div.member");
});
(function($) {
  
  $.fn.randomize = function(tree, childElem) {
    return this.each(function() {
      var $this = $(this);
      if (tree) $this = $(this).find(tree);
      var unsortedElems = $this.children(childElem);
      var elems = unsortedElems.clone();
      
      elems.sort(function() { return (Math.round(Math.random())-0.5); });  
      for(var i=0; i < elems.length; i++)
        unsortedElems.eq(i).replaceWith(elems[i]);
    });    
  };
})(jQuery);
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers