I'm adding a text input to every option item in a Select2 field with "multiple" enabled and I need to prevent all of the Select2 events when the input is clicked on, both in the dropdown state and in the already selected pill result under any condition.
I've got a start from another similar question and have attempted to extend it to my needs by trying all of the Select2 events but I can't seem to block the event on every single place where the input field is showing.
$('#my-select').select2({
width: '300px',
multiple: true,
templateResult: function (item) {
var $result = $('<span>'+item.text+'</span><input class="input" placeholder="Input Text Here" onclick="test()">');
return $result;
},
templateSelection: function (item) {
var $result = $('<span>'+item.text+'</span><input class="input" placeholder="Input Text Here" onclick="test()">');
return $result;
}
}).on("select2:selecting", function (e) {
console.log("selecting",e.params.args.originalEvent);
if (e.params.args.originalEvent.target.className === 'input') {
e.preventDefault();
}
}
);
This JSFiddle will prevent the Select2 event in the dropdown option, but won't prevent it for an already selected option (where clicking the input will clear the option). Also it won't prevent the event in the pill result's input.