79672977

Date: 2025-06-20 07:10:57
Score: 1
Natty:
Report link
$('#select').select2({
  templateResult: formatOption,
  templateSelection: formatOption
});

    function formatOption(option) {
  if (!option.id) return option.text;
  return $(`<span><img src="${option.element.dataset.img}" style="width: 20px;"/> ${option.text}</span>`);
}
<select id="select">
  <option data-img="avatar1.jpg">John</option>
  <option data-img="avatar2.jpg">Jane</option>
</select>
Reasons:
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: fozoh oliver