2

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!

3 Answers 3

2

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.");
       }
   }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe this helps you:

$("select").multiselect({
    header: "Choose only TEN items!",
    click: function(e){

        if( $(this).multiselect("widget").find("input:checked").length > 10 )
        {
            return false;
        } 
        else
        {
            return true;
        }

    }
});

Comments

0

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');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.