I have used bootstrap select
Now, when I click on an option it should be selected as it is working fine, but when I click on the same option it should be deselected with both click
and Enter
keypress event how it is working on multiple select I just want the same way for single select.
I have tried the following code to select and deselect on click event it's working fine but when I am trying to do it with the keyboard enter key
it's not even getting selected.
Any help appreciated :-)
$('.selectpicker').selectpicker({
liveSearch:true,
showTick:true
});
$(document).on('click', '.dropdown-menu li', function(event){
if($(this).hasClass('active')){
$(this).parent().prev('div').parent().next('select').selectpicker('val',''); }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css" rel="stylesheet"/>
<select class="selectpicker">
<option>Mustard</option>
<option>Ketchup</option>
<option>Relish</option>
</select>