Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is possible by finding the index of the placeholder element in the <code>change</code> event, we do want to make sure we ignore the li element that is currently being dragged. I do this by simply adding a class, you could also use <code>$.data()</code>. If you do not ignore the li element that is currently being dragged you will double count since there is a placeholder and the original element in the ul at the time of <code>change</code>.</p> <p>Code is below and here is the fiddle: <a href="http://jsfiddle.net/yYKmw/16/" rel="nofollow">http://jsfiddle.net/yYKmw/16/</a></p> <h2>HTML</h2> <pre><code>&lt;ul id="sortable"&gt; &lt;li&gt;1&lt;/li&gt; &lt;li&gt;2&lt;/li&gt; &lt;li&gt;3&lt;/li&gt; &lt;li&gt;4&lt;/li&gt; &lt;li&gt;5&lt;/li&gt; &lt;/ul&gt; &lt;div id="output"&gt; &lt;span&gt;&lt;/span&gt; &lt;/div&gt; </code></pre> <h2>CSS</h2> <pre><code>#sortable { list-style-type: none; margin: 1em; padding: 0; width: 30%; float: left; } #sortable li { height: 20px; background-color: khaki; margin: 3px; padding: 2px; } .sortable-placeholder { background-color: red !important; } #output { clear:both; border: thin solid black; height: 200px; width: 200px; color: black; } </code></pre> <h2>JS</h2> <pre><code>$( "#sortable" ).sortable({ placeholder: "sortable-placeholder", tolerance: "pointer", start: function(event, ui) { ui.item.addClass( 'ignore' ) }, change: function(event, ui){ var elements = $('#sortable li').not('.ignore') , placeholderElement = elements.filter( '.sortable-placeholder' ) , index = elements.index( placeholderElement ); $('#output span').html('Current index: ' + index) }, stop: function(event, ui) { ui.item.removeClass( 'ignore' ); } }); </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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