I am working on a selection menu in which I use the Select-2 plugin to customize it as I would like. Thus, I have a problem, I'm trying to recreate the selected items in the input field. Therefore, when you select an item, it appears as a small gray list item in the input field.
Here is the FIDDLE that shows that I have a problem.
Here, this is javascript code, as I create my input field:
var selectMenuItems = [
{id: 'restaurant', value: 'restaurant'},
{id: 'shopping', value: 'shopping'},
{id: 'toilet', value: 'toilet'}
]
function formatSelectItem (selectItem) {
if (!selectItem.id) { return selectItem.value; }
var $selectItem = $(
'<img src="img/markers/'+ selectItem.id.toLowerCase() +'.png"/>' +
'<span class="select-item-text">'+ selectItem.value+"</span>"
);
return $selectItem;
};
$("[name='select']").select2({
placeholder: "Selecteer een categorie",
templateResult: formatSelectItem,
data: selectMenuItems
}).on("change", function(e){
console.log(e);
$('.select-multiple :selected').each(function(i, selected) {
var selectedValue = $(selected).val();
$('.select2-selection__choice').text(selectedValue);
});
});
The problem is that when I select an element, it appears with the text inside the input field, but if I select several elements, all the elements will be changed to this last selected text of the element. How can I bind this .text()to this particular selection element?
Select-2 plugin "Templating", .