HTML Checkboxes in select list

33,365

Solution 1

A cross-browser CSS solution that conform CSS level 2 standard:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>select-multiple</title>
    <style>
       /* option:checked:before { content: "✓" } */
       option:before { content: "☐ " }
       option:checked:before { content: "☑ " }
    </style>
</head>
<body>
<h1>select-multiple</h1>
<select multiple="" size="5" style="width: 200px;">
    <option value="0">Banana</option>
    <option value="1">Cherry</option>
    <option value="2">Lemon</option>
    <option value="3">Banana</option>
    <option value="4">Cherry</option>
    <option value="5">Lemon</option>
    <option value="6">Banana</option>
</select>
</body>
</html>

Fiddle

Solution 2

I tried a lot of js plugin for checkboxes in select list and the best one so far is Bootstrap Multiselect

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>jQuery Multi Select Dropdown with Checkboxes</title>

<link rel="stylesheet" href="css/bootstrap-3.1.1.min.css" type="text/css" />
<link rel="stylesheet" href="css/bootstrap-multiselect.css" type="text/css" />

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="js/bootstrap-3.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap-multiselect.js"></script>

</head>

<body>

<form id="form1">

<div style="padding:20px">

<select id="chkveg" multiple="multiple">

    <option value="cheese">Cheese</option>
    <option value="tomatoes">Tomatoes</option>
    <option value="mozarella">Mozzarella</option>
    <option value="mushrooms">Mushrooms</option>
    <option value="pepperoni">Pepperoni</option>
    <option value="onions">Onions</option>

</select>

<br /><br />

<input type="button" id="btnget" value="Get Selected Values" />

<script type="text/javascript">

$(function() {

    $('#chkveg').multiselect({

        includeSelectAllOption: true
    });

    $('#btnget').click(function(){

        alert($('#chkveg').val());
    });
});

</script>

</div>

</form>

</body>
</html>

Here's the DEMO

Solution 3

Maybe this is not a real answer, but anyway:

That's not a standard HTML control, they built it with a series of text inputs and divs, you'd have to do LOTS of work to reproduce that behavior, so that it works (possibly with any browser)...

I'm not aware of any pre-built helpers for MVC, maybe there is something for JQuery? Anyway, if you can't find one and you had to do that manually, maybe you should choose other means with more standard components (or revert to flash or silverlight which are much more convenitent for this kind of customizations).

Solution 4

You could try just leaving it as is pretty much except add

.ms_selectList{ display: none; } 

Add the above to what you already have for ms_selectList

Then in jquery have a click function of some kind that will make the ms_selectList display clock essentially making it appear to be like a select box when its really just an absolutely positioned div tag with a bunch of checkboxes inside.

And the jquery ought to be something like this,

$(document).ready(function(){

    $('.ctrl').click(function(){
        $('#selectList').toggle();
    });

})
Share:
33,365
Lee Smith
Author by

Lee Smith

Updated on July 28, 2022

Comments

  • Lee Smith
    Lee Smith almost 2 years

    I'm trying to build a checkbox list within a select list (like the one for countries here: link text)

    I'm using Asp.net MVC so it needs to be pure/html &| javascript/JQuery. Is this possable? Or is there already a prebuild one I could download load?

    Thanks

    Ripped HTML/CSS:

     <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    
    <style type="text/css">
    body {  background-color: #FFFFFF; font-family: Tahoma; font-size: 11px; }
    
    /* Control Skin */
    input { font: normal 11px Tahoma; }
    div.ctrl { background-color: window; border: solid 1px ThreeDLightShadow; vertical-align: top; margin: 0; padding: 0;  width: 268px; }
    input.Button { background: #2F89BD url('images/btn_default%20gradient.gif') repeat-x; cursor: pointer;
                    height: 24px; color: #FFF; border: solid 1px #2F82BE; padding: 0 10px; width: auto;
                    overflow: visible; }
    input.TextBox { border: solid 1px ThreeDLightShadow; height: 16px; padding-top: 2px; text-indent: 2px; width: 150px; }
    
    /* For MultiSelectControl */
    .ms_multiSelector { position: absolute; display:inline ; border: 1px solid ThreeDLightShadow; width: 268px; z-index: 100; }
    .MultiSelectControl_txtInput { width: 240px; border: none 0; margin: 5px 0 0 5px; padding: 0; vertical-align: top; }
    .ms_selectListWrapper { padding: 0; margin: 0; }
    .ms_selectList { background-color: #FFFFFF; overflow: auto; height: 265px; _height: 150px; /* For IE 6 */ }
    .ms_chkSelectAll { padding-left: 3px; }
    .selectList_Wrapper { padding: 0; margin: 0; }
    .multiSelectorCountry { position: relative; clear: both; display: none; border: 1px solid ThreeDLightShadow; width: 275px; }
    .multiSelector { position: absolute; visibility: hidden; border: 1px solid ThreeDLightShadow; width: 275px; }
    .mutliselect_container { padding: 0; margin: 0; border: 0; display: inline; }
    .chkSelectAll { padding-left: 3px; }
    .selectList { background-color: #FFFFFF; overflow: auto; height: 200px; _height: 150px; /* For IE 6 */ }
    .chklstSelect INPUT { float: left; width: 20px; /* To align the chkbox text in FF */ }
    .chklstSelect LABEL { text-align: left; float: right; width: 230px; /* To align the chkbox text in FF */ }
    #imgMultiSelectArrow { width: 20px; height: 20px; margin-left: 300px; padding-left: 300px; }
    .multiselect_close { padding: 4px 0; float: right; width: 65px; background: transparent url(https://careers.microsoft.com/images/close_btn.gif) no-repeat 33px center; }
    
    </style>
    
    
    
    
    <title>Untitled Page</title>
    </head>
    <body>
    <div class="ctrl" >
    <input  type="text" value="Software Engineering: Development" readonly="readonly"  class="MultiSelectControl_txtInput"  />
    <img  onmouseover="this.src='https://careers.microsoft.com/Images/mm_dropdown_20x20_hover.gif'" onmouseout="this.src='https://careers.microsoft.com/Images/mm_dropdown_20x20.gif'"  src="https://careers.microsoft.com/Images/mm_dropdown_20x20.gif" alt="Show/Hide" style="height:20px;width:20px;border-width:0px;" />
    </div>
    <div  class="ms_multiSelector">
    
        <div id="selectList" class="ms_selectList">
            <input  type="checkbox" value="allcountry" class="ms_chkSelectAll" />
            <span class="ms_chkSelectAll"  >Select All</span>
    
    <div>
            <input  type='checkbox'  value='1'  />
            <label>Business Services & Administration</label>
            <br />
            <input type='checkbox'  value='37'  />
            <label>Customer Service & Support</label>
            <br />
         </div>
      </div>
    
    </div>
    
    
    </body>
    </html>
    
  • gavenkoa
    gavenkoa almost 11 years
    And also with some Chromium versions. But this is in very old standard which many vendors wouldn't implement completely.