jQuery(document).ready(function($){ // Chosen select $(".chosen-select").chosen({ allow_single_deselect: true, single_backstroke_delete: false }); // Apply chosen class for each post in dropdown $(".ajaxd-posts-table input[name=\"ajax_post[]\"]").each(function() { $(".ajaxd-posts option[value=\""+$(this).val()+"\"]").prop("disabled", true); $(".ajaxd-posts").trigger("chosen:updated"); }); // Add post to group $(".ajaxd-posts").on("change", function(evt, params) { post_id = params.selected; $selected = $(this).find("option:selected"); $table = $(".ajaxd-posts-table"); $table.find(" > tbody tr.ajaxd-placeholder").hide(); $table.find(" > tbody:last").append(""+$selected.text()+""+$selected.data("post-type")+""); $selected.prop("disabled", true); $(this).val("").trigger("chosen:updated"); }); // Make posts table sortable and posts removable $(".ajaxd-posts-table > tbody").sortable({ axis: "y" }).on("click", ".ajaxd-delete", function() { $row = $(this).closest("tr"); $id = $row.find("input[name=\"ajax_post[]\"]").val(); $(".ajaxd-posts option[value=\""+$id+"\"]").prop("disabled", false); $(".ajaxd-posts").trigger("chosen:updated"); $row.remove(); $table = $(".ajaxd-posts-table"); if ( $table.find(" > tbody tr:visible").length == 0 ) $table.find(" > tbody tr.ajaxd-placeholder").show(); }); });