There is a drop-down multy select in which I would like to click on an element to remove it from the selected ones. Out of the box chosen.js apparently can not do this, how can you modify it a bit?
I tried to bind the standard function to elements, but it didn’t work. Please give me a hint
$("body").on("click", ".result-selected", function() {
let index = $(this).attr("data-option-array-index");
let container = $(this).closest(".chosen-container");
let select = container.prev();
if ( select != null && select.hasClass("chosen-select") ) {
console.log(index);
select
.find("option:eq(" + index + ")")
.prop("selected", false);
select.trigger("chosen:updated").trigger('chosen:open');
}
});
2
Answers
In the original code, you were triggering chosen:open after deselecting. This might not always behave as expected because if the dropdown is closed when you deselect, opening it immediately after might not be desired. In most cases, you will want to close the dropdown after making changes, not reopen it.
Try this one, replace your Jquery with this and there was a small issue in your css too. So, I have added it at the end of your css.