Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to swap 2 items using Draggable and Droppable?
    primarykey
    data
    text
    <h2>Problem</h2> <p>I have 4 images. User can drag and drop them to another list and change the order of the images. Target is that the users selects the order of the images. The problem is that I am unable to swap these images when they are place on each other.</p> <h2>HTML</h2> <pre><code>&lt;ul id="choices"&gt; &lt;li&gt;&lt;img src="http://placehold.it/200x200&amp;text=1" /&gt;&lt;/li&gt; &lt;li&gt;&lt;img src="http://placehold.it/200x200&amp;text=2" /&gt;&lt;/li&gt; &lt;li&gt;&lt;img src="http://placehold.it/200x200&amp;text=3" /&gt;&lt;/li&gt; &lt;li&gt;&lt;img src="http://placehold.it/200x200&amp;text=4" /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;ul id="answers"&gt; &lt;li&gt;&lt;/li&gt; &lt;li&gt;&lt;/li&gt; &lt;li&gt;&lt;/li&gt; &lt;li&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <h2>jQuery</h2> <pre><code>(function ($) { $("#choices li img").draggable({ revert: true, zIndex: 10, snap: "#answers li", snapMode: "inner", snapTolerance: 40 }); $("#answers li").droppable({ drop: function (event, ui) { var dropped = ui.draggable; var droppedOn = this; if ($(droppedOn).children().length &gt; 0) { alert("I need to swap these"); } $(dropped).detach().css({ top: 0, left: 0 }).prependTo($(droppedOn)); } }); })(jQuery); </code></pre> <h2>CSS (not realy important)</h2> <pre><code>img { display: block; z-index: 3; } #choices, #answers { display:block; padding: 0; margin: 0; } #choices li, #answers li { display: inline-block; height: 200px; width: 200px; margin: 10px; background: #515151; } #answers li { position: relative; } </code></pre> <h2>Example</h2> <p><a href="http://jsfiddle.net/K6QNg/" rel="nofollow">http://jsfiddle.net/K6QNg/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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