select2 add image to selected value code example
Example: add image in select2 dropdown
$(".class").select2({
templateResult: formatState,
templateSelection: formatState
});
function formatState (opt) {
if (!opt.id) {
return opt.text.toUpperCase();
}
var optimage = $(opt.element).attr('data-image');
console.log(optimage)
if(!optimage){
return opt.text.toUpperCase();
} else {
var $opt = $(
' ' + opt.text.toUpperCase() + ''
);
return $opt;
}
};