2
votes

I'm using jquery Draggables with a Sortable. I have a list of elements on the left that I need to drag and drop into different divs on the right. It's possible to put the the same items from the left into multiple boxes, so I used helper: "clone." All works well until I need to get a reference to the new element that was cloned in the receive event so I can add a click handler to it.

I need a reference to the new element!

<div>
        <div style="float: left; width: 300px;">
            <div id="FieldList">
                <h2>Loading</h2>            
            </div>
            <input type="button" name="cmdTest" id="cmdTest" value="Test" />
    </div>
    <div style="float: right; width: 300px;">
        <div>
            <div>Select Columns</div>
            <div class="Toolbar"></div>
            <div id="SelectFields" class="DroppableField">

            </div>
        </div>
        <div>
            <div>Conditions</div>
            <div class="Toolbar"></div>
            <div class="DroppableField" style="background-color: Gray; height: 200px;">

            </div>
        </div>
        <div>
            <div>Order</div>
            <div class="Toolbar"></div>
            <div class="DroppableField" style="background-color: Gray; height: 200px;">

            </div>
        </div>
    </div>
    <div style="clear: both;">
    </div>
</div>

Javascript:

$(document).ready(function () {

        $(".DroppableField").sortable({
            revert: true,
            receive: function (ui, event) { 
                // ui.helper always seems to be null. Many of the other properties seem to reference the original item.  I need the NEW ONE!!!                  
                $(ui.helper).dblclick(function () {

                });
            }
        });

        $.ajax({
            url: "/condition/getconditions",
            data: {},
            dateType: "json",
            type: "POST",
            success: function (data, successcode, jqXHR) {
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    $("#FieldList").append("<div>" + data[i].Description + "</div>").find("div:last").data("fielddata", data[i]);
                    //html += ;
                }

                $("#FieldList div").draggable({
                    connectToSortable: ".DroppableField",
                    helper: "clone",
                    revert: "invalid"
                });
            }
        });

    });
1
There doesn't seem to be an out of the box method to get a reference to the newly cloned item, however, stackoverflow.com/questions/4973339/… offers a nice workaround to this.Björn
Also, you have your ui and event switched around, it should be receive: function (event, ui) Björn

1 Answers

1
votes

You seem to have inverted the two parameters the callback functions receives.

receive: function (ui, event) { 

}

Should be

receive: function (event, ui) { 

}

According to the jQuery UI Docs:

All callbacks receive two arguments: The original browser event and a prepared ui object[...]