$(function() { refreshLocationAlerts(); $(document).on("click", "#not-selected-locations-list-group button", handleSelectButton); $(document).on("click", "#selected-locations-list-group button", handleDeselectButton); }); function handleSelectButton() { $(this).text("Deselect"); let lgi = $(this).closest(".list-group-item").detach(); $("#selected-locations-list-group").append(lgi); let lgis = $("#selected-locations-list-group .list-group-item"); lgis.sort(function(a, b) { let aName = $(a).data("name").toLowerCase(); let bName = $(b).data("name").toLowerCase(); if(aName == bName) return 0; return (aName > bName) ? 1 : -1; }).detach(); $("#selected-locations-list-group").append(lgis); refreshLocationAlerts(); refreshLocationIds(); } function handleDeselectButton() { $(this).text("Select"); let lgi = $(this).closest(".list-group-item").detach(); $("#not-selected-locations-list-group").append(lgi); let lgis = $("#not-selected-locations-list-group .list-group-item"); lgis.sort(function(a, b) { let aName = $(a).data("name").toLowerCase(); let bName = $(b).data("name").toLowerCase(); if(aName == bName) return 0; return (aName > bName) ? 1 : -1; }); $("#not-selected-locations-list-group").append(lgis); refreshLocationAlerts(); refreshLocationIds(); } function refreshLocationIds() { let lgis = $("#selected-locations-list-group .list-group-item"); let ids = []; lgis.each(function(index, lgi) { ids.push($(lgi).data("location-id")); }); $("#location-ids").val(ids.join(", ")); } function refreshLocationAlerts() { let count = 0; count = $("#not-selected-locations-list-group .list-group-item").length; if(count > 0) { $("#not-selected-locations-alert").hide(); } else { $("#not-selected-locations-alert").show(); } count = $("#selected-locations-list-group .list-group-item").length; if(count > 0) { $("#selected-locations-alert").hide(); } else { $("#selected-locations-alert").show(); } }