<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jquery shuffling &#8211; Sibeesh Passion</title>
	<atom:link href="https://sibeeshpassion.com/tag/jquery-shuffling/feed/" rel="self" type="application/rss+xml" />
	<link>https://sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Mon, 17 Aug 2015 08:05:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Jquery shuffling &#8211; Sibeesh Passion</title>
	<link>https://sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Shuffle Div Contents</title>
		<link>https://sibeeshpassion.com/shuffle-div-contents/</link>
					<comments>https://sibeeshpassion.com/shuffle-div-contents/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Mon, 17 Aug 2015 08:02:51 +0000</pubDate>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[Jquery shuffling]]></category>
		<category><![CDATA[shuffle div contents]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=9621</guid>

					<description><![CDATA[In this post, we will see how can we shuffle a div contents using jquery. Here the div contents are another set of divs which contain some values. So what we are going to 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. Background Recently I was crating a simple on-line game, in which I was in a need of this requirement. I will share about the game in my next article. Using the code The first step you need to [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this post, we will see how can we shuffle a div contents using jquery. Here the div contents are another set of divs which contain some values. So what we are going to 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. </p>
<p><strong>Background</strong></p>
<p>Recently I was crating a simple on-line game, in which I was in a need of this requirement. I will share about the game in my next article. </p>
<p><strong>Using the code</strong></p>
<p>The first step you need to do  is add reference.</p>
<p>[js]<br />
 &lt;script src=&quot;jquery-2.0.2.min.js&quot;&gt;&lt;/script&gt;<br />
[/js]</p>
<p>Next we will add the div and its contents.</p>
<p>[html]<br />
 &lt;div id=&quot;parent&quot;&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;1&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;2&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;3&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;4&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;5&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;6&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;7&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;8&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;9&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;10&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;11&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;12&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;13&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;14&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;15&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;16&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;17&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;18&lt;/div&gt;<br />
				&lt;div  class=&quot;myInnerDiv&quot;&gt;19&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;20&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;21&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;22&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;23&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;24&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;25&lt;/div&gt;<br />
    &lt;/div&gt;<br />
[/html]</p>
<p>Can we style those divs now?</p>
<p>[css]<br />
 &lt;style&gt;<br />
        #parent {<br />
            width: 27%;<br />
            height: auto;<br />
            padding: 10px;<br />
            float: left;<br />
            margin-left: 5px;<br />
            min-height: 265px;<br />
        }<br />
        .myInnerDiv {<br />
            width: 25px;<br />
            height: 28px;<br />
            padding: 15px;<br />
            background-color: green;<br />
            margin: 3px;<br />
            float: left;<br />
            cursor: move;<br />
        }<br />
    &lt;/style&gt;<br />
[/css]</p>
<p>See your page now.</p>
<div id="attachment_9641" style="width: 346px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_1.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-9641" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_1.png" alt="Shuffle div contents " width="336" height="356" class="size-full wp-image-9641" srcset="/wp-content/uploads/2015/08/Shuffle_div_contents_1.png 336w, /wp-content/uploads/2015/08/Shuffle_div_contents_1-283x300.png 283w" sizes="(max-width: 336px) 100vw, 336px" /></a><p id="caption-attachment-9641" class="wp-caption-text">Shuffle div contents</p></div>
<p>Now we will add a href tag to fire a click event.</p>
<p>[html]<br />
&lt;a href=&quot;#&quot; id=&quot;shuffle&quot;&gt;Shuffle Me&lt;/a&gt;<br />
[/html]</p>
<p>Next is writing the needed scripts.</p>
<p>[js]<br />
&lt;script&gt;<br />
        $(function () {<br />
			$(&quot;#shuffle&quot;).click(function(){<br />
				var maindiv = $(&quot;#parent&quot;);<br />
				var divs = maindiv.children();<br />
				while (divs.length) {<br />
					maindiv.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);<br />
				}<br />
			});<br />
        });<br />
    &lt;/script&gt;<br />
[/js]</p>
<p>In the above code we are getting the contents from the div <em>parent</em> to a variable <em>maindiv</em>, and we find the children of that div and at last in a while loop we will juse select the divs randomly and append to our main div again. </p>
<p>See the output now. </p>
<p><strong>Output</strong></p>
<p><em>When you load first time</em></p>
<div id="attachment_9641" style="width: 346px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_1.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-9641" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_1.png" alt="Shuffle div contents " width="336" height="356" class="size-full wp-image-9641" srcset="/wp-content/uploads/2015/08/Shuffle_div_contents_1.png 336w, /wp-content/uploads/2015/08/Shuffle_div_contents_1-283x300.png 283w" sizes="(max-width: 336px) 100vw, 336px" /></a><p id="caption-attachment-9641" class="wp-caption-text">Shuffle div contents</p></div>
<p><em>When you load second time</em></p>
<div id="attachment_9651" style="width: 347px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_2.png"><img decoding="async" aria-describedby="caption-attachment-9651" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_2.png" alt="Shuffle Div Contents " width="337" height="346" class="size-full wp-image-9651" srcset="/wp-content/uploads/2015/08/Shuffle_div_contents_2.png 337w, /wp-content/uploads/2015/08/Shuffle_div_contents_2-292x300.png 292w" sizes="(max-width: 337px) 100vw, 337px" /></a><p id="caption-attachment-9651" class="wp-caption-text">Shuffle Div Contents</p></div>
<p><em>When you load third time</em></p>
<div id="attachment_9661" style="width: 327px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_3.png"><img decoding="async" aria-describedby="caption-attachment-9661" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Shuffle_div_contents_3.png" alt="Shuffle Div Contents " width="317" height="346" class="size-full wp-image-9661" srcset="/wp-content/uploads/2015/08/Shuffle_div_contents_3.png 317w, /wp-content/uploads/2015/08/Shuffle_div_contents_3-275x300.png 275w" sizes="(max-width: 317px) 100vw, 317px" /></a><p id="caption-attachment-9661" class="wp-caption-text">Shuffle Div Contents</p></div>
<p><strong>Complete Code</strong></p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Shuffle a div contents &#8211; Sibeesh Passion&lt;/title&gt;<br />
    &lt;script src=&quot;jquery-2.0.2.min.js&quot;&gt;&lt;/script&gt;<br />
    &lt;style&gt;<br />
        #parent {<br />
            width: 27%;<br />
            height: auto;<br />
            padding: 10px;<br />
            float: left;<br />
            margin-left: 5px;<br />
            min-height: 265px;<br />
        }<br />
        .myInnerDiv {<br />
            width: 25px;<br />
            height: 28px;<br />
            padding: 15px;<br />
            background-color: green;<br />
            margin: 3px;<br />
            float: left;<br />
            cursor: move;<br />
        }<br />
    &lt;/style&gt;<br />
    &lt;script&gt;<br />
        $(function () {<br />
			$(&quot;#shuffle&quot;).click(function(){<br />
				var maindiv = $(&quot;#parent&quot;);<br />
				var divs = maindiv.children();<br />
				while (divs.length) {<br />
					maindiv.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);<br />
				}<br />
			});<br />
        });<br />
    &lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body id=&quot;body&quot;&gt;<br />
    &lt;div id=&quot;parent&quot;&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;1&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;2&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;3&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;4&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;5&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;6&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;7&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;8&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;9&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;10&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;11&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;12&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;13&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;14&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;15&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;16&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;17&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;18&lt;/div&gt;<br />
				&lt;div  class=&quot;myInnerDiv&quot;&gt;19&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;20&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;21&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;22&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;23&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;24&lt;/div&gt;<br />
                &lt;div  class=&quot;myInnerDiv&quot;&gt;25&lt;/div&gt;<br />
    &lt;/div&gt;<br />
	&lt;a href=&quot;#&quot; id=&quot;shuffle&quot;&gt;Shuffle Me&lt;/a&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p><strong>Conclusion</strong></p>
<p>I hope you liked this article. Please share me your valuable feedback and suggestions. Thanks in advance.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sibeeshpassion.com/shuffle-div-contents/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
