0

I need to select the multiple values from the dropdown.I am using bootstrap css.

This is my dropdown code:

  <select multiple class="dropdown-menu">
        <option value="monday">Monday</option>
        <option value="tuesday">Tuesday</option>
        <option value="wednesday">Wednesday</option>
        <option value="thursday">Thursday</option>
        <option value="friday">Friday</option>
        <option value="saturday">Saturday</option>
        <option value="sunday">Sunday</option>
    </select>

By using the above code i can select multiple options by using CTRL key,but i don't want use ctrl key and i need to select multiple values.

Is it possible to do that with jquery,javascript?

One more thing like Can i select not more than two or three values is it possible?

Many thanks in advance....

Bhairav
  • 159
  • 3
  • 17
  • There is a great plugin for this [Bootstrap-multiselect](https://github.com/davidstutz/bootstrap-multiselect) that lets you create a dropdown with checkboxes to select. Might be worth looking into! =) – Daniel B Jul 09 '15 at 09:20
  • If you really don't want to use Ctrl key to select multiple items, then [bootstrap multiselect plugin](http://davidstutz.github.io/bootstrap-multiselect/) might be able to help you. Check out it's demo. – Patel Jul 09 '15 at 09:21
  • Thanks for the response and suggestion,but i don't want use any check boxes. – Bhairav Jul 09 '15 at 09:28
  • @Bhairav, See my updated code. – SatyaTNV Jul 09 '15 at 09:45

3 Answers3

2

This is one way to do without ctrl click.

$('option').mousedown(function(e) {
e.preventDefault(); 

$(this).prop('selected', $(this).prop('selected') ? false : true);

alert($("#mySelect :selected").length);//select option selected count
//if($("#mySelect :selected").length==2)
//some stuff
   
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple="multiple" id="mySelect">
    <option id="1">Option</option>
    <option id="2">Option</option>
    <option id="3">Option</option>
    <option id="4">Option</option>
</select>

See this link.

Community
  • 1
  • 1
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
1
$('.dropdown-menu option').mousedown(function(e) {
    e.preventDefault();
    $(this).prop('selected', !$(this).prop('selected'));
    return false;
});
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

you can use plugins for dropdowns like "Multiselect" / "chosen" 1. http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/ 2. https://harvesthq.github.io/chosen/

Nikki
  • 137
  • 2
  • 3
  • 17