In this article, we will see how to shuffle div contents using jQuery. Here the div contents are another set of divs that contain some values. So what we will do is, we will add an href and in the click event we will shuffle the entire contents of our main div. I hope you will like this.
Please see this article in my blog.
Background
Recently, I was creating a simple online game for which I needed this. I will share the game in my next article.
Using the code
The first step you need to do is add the reference.
- <script src="jquery-2.0.2.min.js"></script>
- <div id="parent">
- <div class="myInnerDiv">1</div>
- <div class="myInnerDiv">2</div>
- <div class="myInnerDiv">3</div>
- <div class="myInnerDiv">4</div>
- <div class="myInnerDiv">5</div>
- <div class="myInnerDiv">6</div>
- <div class="myInnerDiv">7</div>
- <div class="myInnerDiv">8</div>
- <div class="myInnerDiv">9</div>
- <div class="myInnerDiv">10</div>
- <div class="myInnerDiv">11</div>
- <div class="myInnerDiv">12</div>
- <div class="myInnerDiv">13</div>
- <div class="myInnerDiv">14</div>
- <div class="myInnerDiv">15</div>
- <div class="myInnerDiv">16</div>
- <div class="myInnerDiv">17</div>
- <div class="myInnerDiv">18</div>
- <div class="myInnerDiv">19</div>
- <div class="myInnerDiv">20</div>
- <div class="myInnerDiv">21</div>
- <div class="myInnerDiv">22</div>
- <div class="myInnerDiv">23</div>
- <div class="myInnerDiv">24</div>
- <div class="myInnerDiv">25</div>
- </div>
Can we style those divs now?
- <style>
- #parent {
- width: 27%;
- height: auto;
- padding: 10px;
- float: left;
- margin-left: 5px;
- min-height: 265px;
- }
- .myInnerDiv {
- width: 25px;
- height: 28px;
- padding: 15px;
- background-color: green;
- margin: 3px;
- float: left;
- cursor: move;
- }
- </style>
See your page now.
Shuffle div contents.
Now we will add an href tag to fire a click event.
- <a href="#" id="shuffle">Shuffle Me</a>
Next is writing the necessary scripts.
- <script>
- $(function () {
- $("#shuffle").click(function(){
- var maindiv = $("#parent");
- var divs = maindiv.children();
- while (divs.length) {
- maindiv.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);
- }
- });
- });
- </script>
In the preceding code we are getting the contents from the div parent to a variable maindiv and we find the children of that div and finally in a while loop we will select the divs randomly and append to our main div again.
See the output now.
Output
When you load the first time.
Complete Code
- <!DOCTYPE html>
- <html>
- <head>
- <title>Shuffle a div contents - Sibeesh Passion</title>
- <script src="jquery-2.0.2.min.js"></script>
- <style>
- #parent {
- width: 27%;
- height: auto;
- padding: 10px;
- float: left;
- margin-left: 5px;
- min-height: 265px;
- }
- .myInnerDiv {
- width: 25px;
- height: 28px;
- padding: 15px;
- background-color: green;
- margin: 3px;
- float: left;
- cursor: move;
- }
- </style>
- <script>
- $(function () {
- $("#shuffle").click(function(){
- var maindiv = $("#parent");
- var divs = maindiv.children();
- while (divs.length) {
- maindiv.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);
- }
- });
- });
- </script>
- </head>
- <body id="body">
- <div id="parent">
- <div class="myInnerDiv">1</div>
- <div class="myInnerDiv">2</div>
- <div class="myInnerDiv">3</div>
- <div class="myInnerDiv">4</div>
- <div class="myInnerDiv">5</div>
- <div class="myInnerDiv">6</div>
- <div class="myInnerDiv">7</div>
- <div class="myInnerDiv">8</div>
- <div class="myInnerDiv">9</div>
- <div class="myInnerDiv">10</div>
- <div class="myInnerDiv">11</div>
- <div class="myInnerDiv">12</div>
- <div class="myInnerDiv">13</div>
- <div class="myInnerDiv">14</div>
- <div class="myInnerDiv">15</div>
- <div class="myInnerDiv">16</div>
- <div class="myInnerDiv">17</div>
- <div class="myInnerDiv">18</div>
- <div class="myInnerDiv">19</div>
- <div class="myInnerDiv">20</div>
- <div class="myInnerDiv">21</div>
- <div class="myInnerDiv">22</div>
- <div class="myInnerDiv">23</div>
- <div class="myInnerDiv">24</div>
- <div class="myInnerDiv">25</div>
- </div>
- <a href="#" id="shuffle">Shuffle Me</a>
- </body>
- </html>
I hope you liked this article. Please share with me your valuable feedback and suggestions. Thanks in advance.




Rahul PrajapatPosted Aug 18, 2015, 4:10 PM EDT
Nice article sir
Santhakumar MunuswamyPosted Aug 18, 2015, 10:31 AM EDT
Good one
Sibeesh VenuTOP 100Posted Aug 17, 2015, 9:04 PM EDT
Nipesh Janghel Thank you
Sibeesh VenuTOP 100Posted Aug 17, 2015, 9:04 PM EDT
Shridhar Sharma Thank you
Nipesh JanghelPosted Aug 17, 2015, 10:34 AM EDT
Great it used for tiles!! Thanks Sibeesh Venu
Shridhar SharmaPosted Aug 17, 2015, 6:58 AM EDT
nice sir :)
Sibeesh VenuTOP 100Posted Aug 17, 2015, 6:58 AM EDT
Sandeep Kumar Thank you
Sibeesh VenuTOP 100Posted Aug 17, 2015, 6:58 AM EDT
Rakesh Chavda Thank you
Sibeesh VenuTOP 100Posted Aug 17, 2015, 6:57 AM EDT
Pankaj Kumar Choudhary Thank you
Sibeesh VenuTOP 100Posted Aug 17, 2015, 6:57 AM EDT
Gopi Chand Thank you
Sibeesh VenuTOP 100Posted Aug 17, 2015, 6:57 AM EDT
Nilesh Jadav Thank you
Sandeep KumarPosted Aug 17, 2015, 6:54 AM EDT
nice article sir :)
RakeshPosted Aug 17, 2015, 6:31 AM EDT
Nice work
Pankaj Kumar ChoudharyTOP 100Posted Aug 17, 2015, 5:40 AM EDT
Nice explain sir
Gopi ChandPosted Aug 17, 2015, 5:20 AM EDT
Very interesting
Nilesh JadavPosted Aug 17, 2015, 4:52 AM EDT
Nice one sir
Sibeesh VenuTOP 100Posted Aug 17, 2015, 4:48 AM EDT
Jacob Robinson Thank you
Jacob RobinsonPosted Aug 17, 2015, 4:39 AM EDT
Good Show...
Sibeesh VenuTOP 100Posted Aug 17, 2015, 4:17 AM EDT
Karthikeyan K Thank you
Sibeesh VenuTOP 100Posted Aug 17, 2015, 4:16 AM EDT
Upendra Pratap Shahi Thank you
Karthikeyan KPosted Aug 17, 2015, 4:09 AM EDT
Good one sir..Thanks for share
Upendra Pratap ShahiPosted Aug 17, 2015, 3:47 AM EDT
again a very big blast info.....very useful sir.....thanks for sharing
Sibeesh VenuTOP 100Posted Aug 17, 2015, 2:07 AM EDT
Rajeesh Menoth Thanks buddy
Sibeesh VenuTOP 100Posted Aug 17, 2015, 2:07 AM EDT
Vaikesh K P Thank you
Rajeesh MenothPosted Aug 17, 2015, 2:04 AM EDT
It's your game concept :)..Good One
Vaikesh K PPosted Aug 17, 2015, 2:03 AM EDT
Good Article :)