Convoluted question, I know. I'm working on a small MVC application for the purposes of my own learning and I'm stuck at this point after much SO scouring. My View is such (note that ItemList is a List):
@model PackItOut.ViewModels.IndexViewModel
<ol id="item-list-20">
@foreach (var i in Model.ItemList)
{
<li class="ui-widget-content">
@switch (i.Category)
{
case "Food":
<i class="fa fa-cutlery" aria-hidden="true"></i>
break;
case "Shelter":
<i class="fa fa-tree" aria-hidden="true"></i>
break;
case "First Aid":
<i class="fa fa-medkit" aria-hidden="true"></i>
break;
case "Clothing":
<i class="fa fa-child" aria-hidden="true"></i>
break;
case "Fluid":
<i class="fa fa-tint" aria-hidden="true"></i>
break;
case "Weather":
<i class="fa fa-bolt" aria-hidden="true"></i>
break;
default:
break;
}
@i.Name
</li>
}
</ol>
<button class="btn btn-danger change-item" id="remove-item-btn"><i class="fa fa-trash" aria-hidden="true"></i> Remove Item</button>
The Javascript is such:
$(document).ready(function() {
var theItemList = $("ol[id*='item-list-']");
$(theItemList).selectable({
selected: function(event, ui) {
$(ui.selected).addClass("ui-selected").siblings().removeClass("ui-selected");
},
stop: function () {
var hasSelection = $(".ui-selected");
if (hasSelection != null)
$(".change-item").css('visibility', 'visible');
else
$(".change-item").css('visibility', 'hidden');
}
});
$("#remove-item-btn").on("click", function (event) {
var targetItem = document.getElementsByClassName("ui-selected");
if (targetItem == null)
alert('Please select an item to remove.');
});
});
Ideally, in the .on("click") I'd like to pass in the object represented by the targeted item so I can remove the item from the ViewModel collection but I'm having trouble getting the actual object and not just the HTML. Any suggestions on not only this issue but any other issues you see in this snippet are always welcome. Thanks in advance.