This is a very simple example, but it should be used for several different cases. I took jQuery UI Draggable + Sortable , and in addition to the .sortable()
widget, I bound the .droppable()
widget to the <ul id="sortable">
element. Although this is not the smartest way in the drop
event for a droppable widget, I check the value of the variable to identify the beginning of the moved item. (does this come from a sortable list or from one of my draggable objects?) If this comes from one of the draggables, I change the HTML code to what I want. Otherwise, it remains the same (because you are just sorting the sort order)
HTML
<ul> <li id="draggable" class="ui-state-highlight">Drag me down</li> </ul> <br /> <br /> <br /> <ul id="sortable"> <li class="ui-state-default">Item 1</li> <li class="ui-state-default">Item 2</li> <li class="ui-state-default">Item 3</li> <li class="ui-state-default">Item 4</li> <li class="ui-state-default">Item 5</li> </ul>
Js
$(function () { var origin = 'sortable'; $("#sortable").droppable({ drop: function (event, ui) { if (origin === 'draggable') { ui.draggable.html('<span>Look at this new fancy HTML!</span>'); console.log(ui.draggable); origin = 'sortable'; } } }).sortable({ revert: true }); $("#draggable").draggable({ connectToSortable: "#sortable", helper: "clone", revert: "invalid", start: function () { origin = 'draggable'; } }); });
Pretty boring example, because the new HTML is static. But let me say that you have a special helper for each drag and drop, and this should be your new HTML. Therefore, for example, you store some HTML fragments in an array and select the appropriate record suitable for the index, or something like that. It will look like this:
var helpersArr = ['<input type="text" />', '<input type="file" />', '<input type="submit" value="Save" />']; var helper; //draggable helper: function () { helper = helpersArr[$(this).index()]; return helper; } //dropppable drop function ui.draggable.html(helper);
Pretty sure you can achieve the same result by avoiding the helper variable and use ui.helper
, but I couldn't figure out how to get the HTML part. If this does not work as you need, just let me know. In general, I think that you can use my examples as a starting point for various purposes related to modifying the HTML of the dropped element.
SirDerpington
source share