21

I need to make a subset of the Bootstrap 3 glyphicons selectable in my ui by the user.

I tried this:

<select class="form-control"> <option><span class="glyphicon glyphicon-cutlery"></span></option> <option><span class="glyphicon glyphicon-eye-open"></span></option> <option><span class="glyphicon glyphicon-heart-empty"></span></option> <option><span class="glyphicon glyphicon-leaf"></span></option> <option><span class="glyphicon glyphicon-music"></span></option> <option><span class="glyphicon glyphicon-send"></span></option> <option><span class="glyphicon glyphicon-star"></span></option> </select> 

However all I get are blank lines. Any help or alternative suggestions appreciated.

4 Answers 4

24

To my knowledge the only way to achieve this in a native select would be to use the unicode representations of the font. You'll have to apply the glyphicon font to the select and as such can't mix it with other fonts. However, glyphicons include regular characters, so you can add text. Unfortunately setting the font for individual options doesn't seem to be possible.

<select class="form-control glyphicon"> <option value="">&#x2212; &#x2212; &#x2212; Hello</option> <option value="glyphicon-list-alt">&#xe032; Text</option> </select> 

Here's a list of the icons with their unicode:

http://glyphicons.bootstrapcheatsheets.com/

Sign up to request clarification or add additional context in comments.

6 Comments

Note: This trick also works with font-awesome glyphicons <select class="form-control fa"><option value="">&#xf1ab;&nbsp; demo</option></select>. Instead of using the class glyphicon, use the class fa. You can get the list of unicode for the icons here: fontawesome.io/cheatsheet
This worked partially with BS 3.3.7. For example, &#x270f; is showing up but not &#xe023; or &#xe013;.
Applying glyphicon on the <select> caused the text to be shown with different font. I have both, icon + font in <option> tag. And no icon is showing up.
If you go with font-awsome remember that by just using the fa class in select it will use the standard icon size. if you prefer a larger icon use 'fa fa-lg' in the select class
This should be the accepted answer. It adequately answers the question without including additional libraries or plugins. Have my upvote.
|
22

I don't think the standard HTML select will display HTML content. I'd suggest checking out Bootstrap select: http://silviomoreto.github.io/bootstrap-select/

It has several options for displaying icons or other HTML markup in the select.

<select id="mySelect" data-show-icon="true"> <option data-content="<i class='glyphicon glyphicon-cutlery'></i>">-</option> <option data-subtext="<i class='glyphicon glyphicon-eye-open'></i>"></option> <option data-subtext="<i class='glyphicon glyphicon-heart-empty'></i>"></option> <option data-subtext="<i class='glyphicon glyphicon-leaf'></i>"></option> <option data-subtext="<i class='glyphicon glyphicon-music'></i>"></option> <option data-subtext="<i class='glyphicon glyphicon-send'></i>"></option> <option data-subtext="<i class='glyphicon glyphicon-star'></i>"></option> </select> 

Here is a demo: https://www.codeply.com/go/l6ClKGBmLS

3 Comments

Thank you, the first part answers my question, technically it's a font in BS3 so I think I'll pursue that aspect of it to see if I can get a regular select working with it as a font. Your solution does technically show the icon in the select drop down list it doesn't in the edit portion and I'm worried it won't work fully support BS3 since it appears that control pre-dates bs3 and they make no mention in their docs about it.
You're welcome. I haven't seen any mention of BS 3 for Bootstrap Select either.. hopefully they'll update it.. it's a great plugin
the bootply.com seems down. Is there any way I can replace the caret with my glyph icon?
20

I ended up using the bootstrap 3 dropdown button, I'm posting my solution here in case it helps someone in future. Adding the bootstrap 3 list-inline to the class for the ul causes it to display in a nicely compact format as well.

<div class="btn-group"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Select icon <span class="caret"></span> </button> <ul class="dropdown-menu list-inline" role="menu"> <li><span class="glyphicon glyphicon-cutlery"></span></li> <li><span class="glyphicon glyphicon-fire"></span></li> <li><span class="glyphicon glyphicon-glass"></span></li> <li><span class="glyphicon glyphicon-heart"></span></li> </ul> </div> 

I'm using Angular.js so this is the actual code I used:

<div class="btn-group"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Avatar <span class="caret"></span> </button> <ul class="dropdown-menu list-inline" role="menu"> <li ng-repeat="avatar in avatars" ng-click="avatarSelected(avatar)"> <span ng-class="getAvatar(avatar)"></span> </li> </ul> </div> 

And in my controller:

$scope.avatars=['cutlery','eye-open','flag','flash','glass','fire','hand-right','heart','heart-empty','leaf','music','send','star','star-empty','tint','tower','tree-conifer','tree-deciduous','usd','user','wrench','time','road','cloud']; $scope.getAvatar=function(avatar){ return 'glyphicon glyphicon-'+avatar; }; 

2 Comments

This helped me with the angular version. Thanks a lot. :)
Similarly, this approach works with Ember/Handlebars too.
1

If you are using the glyphicon as the first character of the string, you can use the html char (see https://glyphicons.bootstrapcheatsheets.com/) and then apply the font to the first character of the element:

 option::first-letter{ font-family: Glyphicons Halflings; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.