I just started using project for showing multiple tags from a select box and it works great, thanks for the library.
I just need to modify the color or css of the tags shown in multi-value select-boxes. Right now the color of the tag is shown as grey and I would like to change that to some other color according to the type of the tag. Or at least is there a way to change the default color?
Also is it possible to change the css class of tags? There is an option such as formatResultCssClass but when I tried to add css classes through that property nothing changed, I would appreciate if someone can show an example how to do this?
Edit: Workaround for solving the problem:
Add a new property to the select2.defaults for representing classes of selected objects.
$.fn.select2.defaults = {
...
selectedTagClass: "",
...
}
addSelectedChoice: function (data) {
var choice=$(
"<li class='select2-search-choice " + this.opts.selectedTagClass + "'>" +
" <div><a href='#' onclick='return false;' class='select2-search-choice-close' tabindex='-1'><i class='icon-remove icon-white'/></a></div>" +
"</li>"),
id = this.id(data),
val = this.getVal(),
formatted;
...
And initialize select2 using this new property:
$(".#select2Input").select2({placeholder: "Please Select Country",
selectedTagClass: 'label label-info', // label label-info are css classes that will be used for selected elements
formatNoMatches: function () { return "There isn't any country similar to entered query"; }
});
First up – a warning that this means you are overriding the CSS that is internal to select2, so if select2 code changes at a later date, you will also have to change your code. There is no
formatChoiceCSSmethod at the moment (though it would be useful).To change the default color, you will have to override the various CSS properties of the tag which has this CSS class:
To change the class of each tag based on the text or option # of that tag, you will have to add a change event:
And you can define a custom background-color etc in this class: