18

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

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

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/

Zoe
  • 27,060
  • 21
  • 118
  • 148
monokh
  • 1,970
  • 3
  • 22
  • 31
  • 1
    I think there is more simple solutions, but you can have a look at this amazing jquery plugins: http://ivaynberg.github.io/select2/index.html, the templating part can interest you. – Getz May 13 '13 at 15:26
  • Check this http://stackoverflow.com/questions/2965971/how-to-add-a-images-in-select-list – PSL May 13 '13 at 15:33
  • @PSL I actually saw that before i posted thats why i mentioned 'a manual CSS background for each option' but I would prefer to use the font-awesome framework. – monokh May 13 '13 at 15:58

5 Answers5

19

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>
Sanojian
  • 199
  • 1
  • 3
5

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>
Billy Moat
  • 20,792
  • 7
  • 45
  • 37
  • 2
    Yeah, you're right. I knocked it up in Firefox and it works there but not in Chrome. Ah well. – Billy Moat May 13 '13 at 15:32
  • Thanks for the solution, i tried this in chrome originally and thought it didn't work, but would need a cross browser solution. I think i have to go for a simple background image – monokh May 13 '13 at 16:01
2

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.

$(function(){
  $("#category li a").click(function(){
    $(".category").val($(this).text());
  });
});

Hope that help shed some light.

Jennift
  • 667
  • 1
  • 6
  • 13
0

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>
Jeremy Lynch
  • 6,780
  • 3
  • 52
  • 63
0

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
});
Community
  • 1
  • 1
Jayden Lawson
  • 2,154
  • 2
  • 21
  • 24