I'm using this jQuery UI Multiselect script: http://www.quasipartikel.at/multiselect/index.html in project and I need a little help with it. How do I limit the max number of items that can be selected... to 10 items let's say!?
Thanks!
I'm using this jQuery UI Multiselect script: http://www.quasipartikel.at/multiselect/index.html in project and I need a little help with it. How do I limit the max number of items that can be selected... to 10 items let's say!?
Thanks!
this is an old thread, but for anybody coming across this problem - here is the official method to limit the number of checkbox rows selected for the Hynds version
NOTE: I would recommend a message class to appear for user's who try selecting more than the maximum allowed. Also, ">10" is counted from ZERO '0' being the fist index value, meaning selections 0,1,2,3,...9 = 10 maximum in this example.
Answer:
var warning = $(".message");
$("select").multiselect({
header: "Choose only TEN items!",
click: function(e){
if( $(this).multiselect("widget").find("input:checked").length > 10 ){
warning.addClass("error").removeClass("success").html("You can only check ten checkboxes!");
return false;
} else {
warning.addClass("success").removeClass("error").html("Check a few boxes.");
}
}
});
Note that you can also disable the remaining items once you've reached the maximum you want selected, this will prevent the user to add more, thus having the effect you want :
$('#multiselect').bind('multiselectChange', function(evt, ui) {
var selectedCount = $("option:selected", this).length;
$(this)
.find('option:not(:selected)').prop('disabled', selectedCount >= MAXIMUM_SELECTED_HERE).end()
.multiselect('refresh');
});