Here is my HTML code:
<select class="select2">
<option value="1" style="background-color: #ff0000;">OPTION 1</option>
<option value="2" style="background-color: #ffffff;">OPTION 2</option>
....
<option value="144" style="background-color: #000000;">OPTION 144</option>
</select>
and finally, I use select2 on print this select list:
$('.select2')select2();
The problem is with style attribute - when I set this attribute into every option, jQuery script doesn't copy this attribute for <li> lists.
I know, I can set every background-color into <style></style> tags, but I have more options (~150) and I manage this attribute (background-color) on my site as live (when user change some options, then I change background-color), so I need solution which will be live-copy style attribute from original options list and when I reset select list (using $('.select2').select2()), bg color will be reset also.
How I can do it?
@UPDATE
I solved problem in that way:
function formatState(data, container)
{
if (data.element)
{
$(container).css('background-color', '#'+$(data.element).attr('data-style'));
}
return data.text;
}
$('.select2').select2({templateResult: formatState});
and I replace options HTML code:
<option value="1" style="background-color: #ff0000;">OPTION 1</option>
for this:
<option value="1" data-style="ff0000">OPTION 1</option>