Modifying attributes of items as they are dropped into a sortable list with jQuery
<p>I have a list of items that are draggable from a source list to a destination list contained in a form element. This works fine for one destination list as the 'name' attribute can be specified statically. However, I'm trying to figure out a way to support multiple destination lists, and I thought that if I could grab the ID from the destination list and dynamically add that as the name attribute to an input field as it is dropped into the sortable list, each list could be submitted as a separate array. My sortable, draggable, and droppable functions are working exactly the way I want them to with the following code:</p>

<pre><code>    &lt;div&gt;
      Available Items
      &lt;ul id="avail"&gt;
        &lt;li&gt;&lt;input type="hidden" name="options[]" value="1"&gt;Option 1&lt;/li&gt;
        &lt;li&gt;&lt;input type="hidden" name="options[]" value="2"&gt;Option 2&lt;/li&gt;
        &lt;li&gt;&lt;input type="hidden" name="options[]" value="3"&gt;Option 3&lt;/li&gt;
        &lt;li&gt;&lt;input type="hidden" name="options[]" value="4"&gt;Option 4&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;p class="clear"&gt;&lt;/p&gt;
    &lt;form action="myForm.php" method="post" enctype="multipart/form-data"&gt;
      &lt;div style="float:left;"&gt;
        List #1
        &lt;ul class="sortable" id="list1"&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
      &lt;div style="float:left;margin-left:5px;"&gt;
        List #2
        &lt;ul class="sortable" id="list2"&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
      &lt;div class="clear"&gt;&lt;/div&gt;&lt;br&gt;
    &lt;/form&gt;
</code></pre>

<p>and the following jQuery:</p>

<pre><code>    $('#avail li').draggable({
      connectToSortable: ".sortable",
      helper: "clone",
      opacity: 0.75,
    });
    var sortableIn;
    $('.sortable').sortable({
      receive: function(event, ui) {
        sortableIn = 1;
      },
      over: function(event, ui) {
        sortableIn = 1;
      },
      out: function(event, ui) {
        sortableIn = 0;
      },
      beforeStop: function(event, ui) {
        if (sortableIn == 0) {
          ui.item.remove();
        }
      }
    }).droppable({
      greedy: true
    });
</code></pre>

<p>You can see it in action on <a href="http://jsfiddle.net/1zo13gc8/5/" rel="nofollow">this fiddle</a>. So to summarize based on this example, I'm trying to replace 'option[]' with either 'list1[]' or 'list2[]' as the name attribute of the input field when an option is dropped into one of these lists.</p>

<p>I assume I'd need to use something like attr('name',$(this).attr('id')) in the "receive" function or in a "drop" function but I'm not exactly sure where to put it and how to define the input field as the target of the attr() change instead of the containing <code>&lt;li&gt;</code>.</p>

<hr>

<p><strong>RESOLUTION:</strong>
I changed the <code>receive</code> function to <code>update</code> and added Twisty's second code snippet to it so that it now looks like:</p>

<pre><code>update: function(event, ui) {
   sortableIn = 1;
   ui.item.find("input").attr("name", $(this).attr("id") + '[]');
}
</code></pre>