For a Pure CSS approach, you can use the :checked
selector combined with the ::before
selector to inline conditional content.
Just add the class select-checkbox
to your select
element and include the following CSS:
<!-- language: lang-css -->
.select-checkbox option::before {
content: "\2610";
width: 1.3em;
text-align: center;
display: inline-block;
}
.select-checkbox option:checked::before {
content: "\2611";
}
You can use plain old unicode characters (with an escaped hex encoding) like these:
Or if you want to spice things up, you can use these FontAwesome glyphs
Demo in jsFiddle & Stack Snippets
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-css -->
select {
width: 150px;
}
.select-checkbox option::before {
content: "\2610";
width: 1.3em;
text-align: center;
display: inline-block;
}
.select-checkbox option:checked::before {
content: "\2611";
}
.select-checkbox-fa option::before {
font-family: FontAwesome;
content: "\f096";
width: 1.3em;
display: inline-block;
margin-left: 2px;
}
.select-checkbox-fa option:checked::before {
content: "\f046";
}
<!-- language: lang-html -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<h3>Unicode</h3>
<select multiple="" class="form-control select-checkbox" size="5">
<option>Dog</option>
<option>Cat</option>
<option>Hippo</option>
<option>Dinosaur</option>
<option>Another Dog</option>
</select>
<h3>Font Awesome</h3>
<select multiple="" class="form-control select-checkbox-fa" size="5">
<option>Dog</option>
<option>Cat</option>
<option>Hippo</option>
<option>Dinosaur</option>
<option>Another Dog</option>
</select>
<!-- end snippet -->
Note: Beware of IE compatibility issues however