Icon in option – Bootstrap + Font-awsome

Icon in option – Bootstrap + Font-awsome

I am trying to embed an icon in an option from a select list. Using font-awesome icons, no icon is being displayed.

Can i use font-awesome to achieve what i need? Or do i have to scrap using font-awesome and do a manual CSS background for each option?
JSFiddle: http://jsfiddle.net/mmXh2/1/

Solutions/Answers:

Solution 1:

You can use FontAwesome as a unicode font and insert your icons in a cross-platform way. You just need to look up the unicode value for each icon

<select style="font-family: 'FontAwesome', Helvetica;">
    <option>&#xf083; Now I show the pretty camera!</option>
</select>

Solution 2:

You can cheat a little bit and put the class on the option:

http://jsfiddle.net/mmXh2/2/

<option class="icon-camera-retro"> Doesn't work in option!</option>

Solution 3:

Apparently Select2 (http://ivaynberg.github.io/select2/) is a solution to putting icons in option tags. However, perhaps due to my lack of knowledge, I just couldn’t make it work. In the end I resorted to using lists (I was also using Bootstrap)

<a class="btn dropdown-toggle category" data-toggle="dropdown" href="#">All Categories <span class="caret pull-right"></span></a>
<ul id="category" class="dropdown-menu">
    <li><a href="#"><i class="icon"></i> Category A</a></li>
    <li><a href="#"><i class="icon"></i> Category B</a></li>
    ..
</ul>

There is a drawback though, the id of the list has to be unique. So, if like me you had 5 different lists in one page, you have to declare (?) all of them in javascript making the codes chunky.

Related:  Fluid or fixed grid system, in responsive design, based on Twitter Bootstrap
$(function(){
  $("#category li a").click(function(){
    $(".category").val($(this).text());
  });
});

Hope that help shed some light.

Solution 4:

Use a plugin like select2

Here’s a working jsfiddle

<select id="icon_select" style="width: 150px;"><option value="fa-glass">&amp;#xf000; fa-glass</option>
  <option value="fa-music">&amp;#xf001; fa-music</option>
</select>

Solution 5:

A solution for icons working in Chrome has been given on a similar question.

JSFiddle Example

Code used:

function format(icon) {
    var originalOption = icon.element;
    return '<i class="fa ' + $(originalOption).data('icon') + '"></i> ' + icon.text;
}
$('.wpmse_select2').select2({
    width: "100%",
    formatResult: format
});

References