I had a similar problem that required a far more elaborate outcome. My system was dragging from a div on the left that contained other divs with form element names and ids, which dropped into a sortable div on the right that was to become a webform. Because I needed the div on the right to closely resemble the contents of a form without extra markup, using an unordered list was out of the question unless I wanted to do a bunch of "scrubbing" on the way into the database. No thanks. In my example, when I drag from the list on the left, I have to use the id of the dragged element to look up a number of things via ajax, and then place a label, form element, and validation into the list on the left based on the results of that ajax. For clarity's sake, I removed the ajax from my example.
So, in essence, you're taking two side-by-side divs, doing draggable and sortable with list connection. The key is that there are callback functions in Sortable that you have to make use of. The "Receive" callback will fire only when a new element is added to the sortable list. This is important, because you have to differentiate so your change doesn't occur on sort as well. You can't solely use "receive" though, because if you try to manipulate the html in that callback, you'll affect the list your dragging from, not the list you're dragging TO. To keep track of whether it's a list add or a drag, I set a variable called "newlement" in $.data to 1 if its a list add, which I then check in the update callback to see if I should effect the dragged html or not. In my case, I don't want to do anything to an element that's dragged.
So, the HTML:
<div class='master_list'>
<div id="2">First Name</div>
<div id="3">Last Name</div>
</div>
<div id="webform_container">
</div>
And the jQuery:
$( ".master_list div" ).draggable({
connectToSortable: "#webform_container",
helper: "clone",
revert: "invalid"
});
$( "#webform_container" ).sortable({
revert: true,
cursor: 'pointer',
placeholder: "placeholderdiv",
connectWith: '.master_list',
start: function() {
/* Clear the value of newelement at start */
$(this).data('newelement', 0);
},
receive: function(event, ui) {
/* Get id from function and pass to the update to do stuff with */
$(this).data('id', ui.item[0].id);
/* Set value of newelement to 1 only when new element is added to list */
$(this).data('newelement', 1);
},
update: function(event, ui) {
/* If newelement ==1, this is a new element to the list, transform it */
if ($(this).data('newelement') == 1) {
var fieldname = ui.item.text();
ui.item.html('your new html here');
}
}
});