How to add icon in select2?
Asked Answered
T

6

12

I using select2 version 4

I try like this :

$("select02").select2({
    placeholder: "<i class='fa fa-sitemap'></i>Branch name",
});

But, it's not working

Demo is like this : http://jsfiddle.net/fc1qevem/8/

Any solution to solve my problem?

Teilo answered 23/5, 2016 at 8:37 Comment(2)
Can you post the rest of your code? Is the stylesheet attached correctly?Marthamarthe
@KarlGjertsen, jsfiddle.net/fc1qevem/8Teilo
A
20

Always read the reference/manual first!

From the official select2 examples on their web site;

function formatState (state) {
  if (!state.id) { return state.text; }
  var $state = $(
    '<span><img src="vendor/images/flags/' +  state.element.value.toLowerCase() + 
'.png" class="img-flag" /> ' + 
state.text +     '</span>'
 );
 return $state;
};

$(".js-example-templating").select2({
  templateResult: formatState
});

Gives you the result:

Select2 with icons

Aid answered 23/5, 2016 at 8:48 Comment(3)
This is not what he asked for. He wants to have an icon in the search field.Communard
Thank you guy! I loved your response;Teevens
Always read the question before you answer :)Anurous
D
8

Found this How to add html placeholder to select2?

So what you need is:

$("select02").select2({
  placeholder: '<i class="fa fa-sitemap"></i>Branch name',
  escapeMarkup : function(markup) {
    return markup;
  }
});
Daggna answered 21/11, 2017 at 5:57 Comment(1)
this is the solution works for put icon inside the select2 both two attributes are needed to render the iconSchutz
M
4

For Icons instead of img use:

function format (state) {
    if (!state.id) { return state.text; }
    return '<i class="fa fa-lg '+state.id.toLowerCase()+'"></i> '+state.text;
} 

$("#select2icon").select2({
    formatResult: format,
    formatSelection: format,
    escapeMarkup: function(m) { return m; }
});
Miranda answered 27/11, 2018 at 3:42 Comment(0)
V
3

Per the migration guide, versions after 3.5.x no longer use formatResult or formatSelection. These have been replaced with templateResult and templateSelection.

function iconFromValue(val){
    if(val.element){
        val = `<span class="select2-option-img"><img src="${val.element.value}"><span> ${val.text}`;
    }
    return val;
}

$(document).ready(function() {
    $('.select2').select2({
        templateResult: iconFromValue,
        templateSelection: iconFromValue,
        escapeMarkup: function(m) { return m; }
    });
}
Vat answered 12/12, 2020 at 18:39 Comment(0)
M
0

Here is working example:

function format(state) {
    if (!state.id) 
        return state.text; // optgroup
    return "<img class='flag' src='images/flags/" + state.id.toLowerCase() + ".png'/>" + state.text;
}

$("#select2").select2({
    formatResult: format,
    formatSelection: format,
    escapeMarkup: function(m) { return m; }
});

user above js.

Miranda answered 27/11, 2018 at 3:39 Comment(0)
B
0

You almost had it. You just need to add 'escapeMarkup'. See the code below.

var select02 = $('#select02');

$(select02).select2({
    data: [{
        id: 0,
        text: "test01"
    }, {
        id: 1,
        text: "test02"
    }, {
        id: 2,
        text: "test03"
    }, {
        id: 3,
        text: "test04"
    }, {
        id: 4,
        text: "test05"
    }],
     placeholder: "<i class='fa fa-sitemap'></i>Branch name",
     escapeMarkup: function (markup) { return markup; }
});
Bettiebettina answered 13/12, 2018 at 3:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.