Mahesh Reddy
Mahesh Reddy

Reputation: 356

select2-jquery: How to set maximumselectionsize dynamically

I've a requirement where I'll decide Max selections to be allowed in select2 ui.How can I do that. I tried by

$('#ChnageLnk').on('click',function(){
$('#SelectD').select2({maximumselectionsize :1});
});

Upvotes: 2

Views: 3411

Answers (5)

Muhammad Jamil AJ
Muhammad Jamil AJ

Reputation: 1

This works for me:

$('#ChnageLnk').on('click',function () {
    $('SelectD').select2({ //re-init
        maximumSelectionLength: 1
    });
});

Upvotes: 0

Vladimir Krasikov
Vladimir Krasikov

Reputation: 23

This should works with the old 3.5 library

if($('#SelectD').data('select2') !== 'undefined') {
   $('#SelectD').data('select2').opts.maximumSelectionSize = 4;
}

Upvotes: 0

ivan.mylyanyk
ivan.mylyanyk

Reputation: 2101

Please, note that since version 4.0 of select2 they have changed maximumSelectionSize to maximumSelectionLength.

You can find more in this question: maximumSelectionSize isn't working in Select2

Upvotes: 10

Mahesh Reddy
Mahesh Reddy

Reputation: 356

$('#ChnageLnk').on('click',function(){ $('#SelectD').select2({ maximumSelectionSize: 1 }); });

Upvotes: 0

mccannf
mccannf

Reputation: 16659

Edit

Sorry - just checked. The option should not be all lowercase - it should be camel case like so:

$('#ChnageLnk').on('click',function(){
   $('#SelectD').select2({ maximumSelectionSize: 1 });
});

Fiddle here.

Upvotes: 5

Related Questions