Trying to implement a custom select dropdown using select2 plugin Is it possible to have the selected value to display only the actual option \'value\' instead of the text,
The version 4 of Select 2 uses templateSelection
instead of formatSelection
:
https://select2.github.io/announcements-4.0.html#changed-templating
https://select2.github.io/options.html#templateSelection
$element.select2({
/**
* @param {Object} item
* @param {Boolean} item.disabled
* @param {HTMLOptionElement} item.element
* @param {String} item.id
* @param {Boolean} item.selected
* @param {String} item.text
* @returns {String}
*/
templateSelection: function(item) {
/** @type {jQuery} HTMLOptionElement */
var $option = $(item.element);
var $optGroup = $option.parent();
return $optGroup.attr('label') + ' (' + item.text + ')';
}
});