I have two UL on my page. UL1 and UL2. Both are sortable and draggable. When the LI from UL1 is added to UL2, the LI leaves UL1. To remove from UL2, click on the delete link inside LI. Logic deletes an element and adds it back to UL1 from UL2.
This is my code:
$(".xOut").on("click", function(){
var id = $(this).attr('id');
$.ajax({
url: '@Url.Action("RemoveCommand")',
type: "POST",
data: {
aid: id,
bId: @(Model.Id)
},
success: function(event, ui){
var newListItem = $("<li />", {
html: $("#"+id).html()
});
$(newListItem).attr("id", id);
newListItem.appendTo("#sortable1");
$("#"+id).remove();
}
});
});
The code works fine if I do not add this line:
$(newListItem).attr("id", id);
I need the new element to have an identifier and cannot work without it.
How to do it?
Update Added HTML code:
<ul id="sortable1" class='droptrue'>
@for (var i = 0; i < Model.SomeList.Count(); i++)
{
var item = @Model.SomeList.ElementAtOrDefault(i);
if (item != null)
{
<li id="@(item.Id)">
<div>@item.Name</div>
</li>
}
}
</ul>
<ul id="sortable2" class='droptrue'>
@for (var i = 0; i < Model.SomeAssignedList.Count(); i++)
{
var item = @Model.SomeAssignedList.ElementAtOrDefault(i);
if (item != null)
{
<li id="@(item.Id)">
<div>@item.Name</div>
</li>
}
}
</ul>
source
share