I recently created a multiselect dropdown menu using Bootstrap Multiselect. I successfully set a limit on the number of options that can be selected (in this case, 5), and once the limit is reached, the additional options become disabled. Everything works perfectly, except when attempting to select more than 5 options using the SHIFT key. In this scenario, my jQuery code to prevent further selection does not work, allowing me to exceed the limit. Any suggestions or solutions would be greatly appreciated.
jQuery('#soft_skill_list').multiselect({
enableFiltering: true,
maxHeight:400,
enableCaseInsensitiveFiltering:true,
nonSelectedText: 'Soft Skills *',
numberDisplayed: 2,
selectAll: false,
onChange: function(option, checked) {
// Get selected options.
var selectedOptions = jQuery('#soft_skill_list option:selected');
if (selectedOptions.length >= 5) {
// Disable all other checkboxes.
var nonSelectedOptions = jQuery('#soft_skill_list option').filter(function() {
return !jQuery(this).is(':selected');
});
nonSelectedOptions.each(function() {
var input = jQuery('input[value="' + jQuery(this).val() + '"]');
input.prop('disabled', true);
input.parent('li').addClass('disabled');
});
}
else {
// Enable all checkboxes.
jQuery('#soft_skill_list option').each(function() {
var input = jQuery('input[value="' + jQuery(this).val() + '"]');
input.prop('disabled', false);
input.parent('li').addClass('disabled');
});
}
}});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />
<select name="soft_skill_list[]" class="soft_skill_list" id="soft_skill_list" multiple="multiple">
<option>Analysing data</option>
<option>Banquets Operations</option>
<option>Concierge Operations</option>
<option>Customer service experience</option>
<option>Measuring and calculating</option>
<option>Micros</option>
<option>Numeracy Skills </option>
<option>Opening Hotels</option>
<option>Opera</option>
<option>Outside catering</option>
<option>Pre-opening</option>
<option>Procedures </option>
<option>Proficiency in computer programming</option>
<option>Public speaking experience </option>
<option>Reservation</option>
<option>Restaurants operations</option>
<option>Revenue Analysis</option>
<option>Rooms Division</option>
<option>Safety and Security</option>
<option>Sales administration</option>
<option>Sales Operations</option>
<option>Social Media</option>
</select>
Please refer to the image here https://i.stack.imgur.com/5l5iX.png