2

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});
});
mccannf
  • 16,619
  • 3
  • 51
  • 63
Mahesh Reddy
  • 356
  • 4
  • 14

5 Answers5

10

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

Community
  • 1
  • 1
ivan.mylyanyk
  • 2,051
  • 4
  • 30
  • 37
5

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.

mccannf
  • 16,619
  • 3
  • 51
  • 63
  • 1
    It's not proper answer ( also, question title isn't relevent ). Because it doesn't "set maximumselectionsize dynamically". It recreates `select2` with one param. If you have set more options-params/have dynamic data/etc. - you lose all. It's not common solution. – voodoo417 Jul 13 '17 at 00:56
0

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

Mahesh Reddy
  • 356
  • 4
  • 14
0

This should works with the old 3.5 library

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

This works for me:

$('#ChnageLnk').on('click',function () {
    $('SelectD').select2({ //re-init
        maximumSelectionLength: 1
    });
});
Qiniso
  • 2,587
  • 1
  • 24
  • 30