I am trying to put a button in select2 result items (to allow the user to delete items). I managed to place the buttons, but I still could not handle their click event. Somehow the event does not rise. I think something like select2 closes the dropdown menu before my button click event goes up, but cannot figure out how I can make it work.
Here is a snippet that I have now.
... formatResult: function (item) { return item.text + "<button class='btn btn-xs btn-default pull-right select2-result-button' data-id='" + item.id + "'>×</button>"; } ... $(document).on("click", ".select2-result-button", function (e) { alert("clicked: " + $(this).data("id")); e.preventDefault(); e.stopPropagation(); return false; });
Here is the fiddle demo. I also tried the mousedown event without success.
javascript jquery jquery-select2
Zoltán tamási
source share