How to dynamically change select2 properties/options in v4.0.0?

530 views
Skip to first unread message

aliham...@gmail.com

unread,
Oct 6, 2017, 3:53:22 AM10/6/17
to select2
Hello,

I need to change the maximumSelectionLength option dynamically in multiple select2 v4.0.0.

For example, if the user chooses 'ALL' value I need to set maximumSelectionLength to 1, in order not to allow user to choose other option than 'ALL', but if the user chooses some other value, I need to keep maximumSelectionLength = 0 (Infinity).

I tried this code:
// select2 initilaization
$('#selector').select2({some options and ajax call})
}).change(function(e) {
$(this).data('select2').options.options.maximumSelectionLength = 1;
console.log($(this).data('select2').options.options);
});;

In console.log I can see the options is changed, but it is not applied to the select2 options.

Knowing that in select2 v3.5.0 I used to this and it was working fine.
I used to write this code:

$(this).data('select2').opts.maximumSelectionSize = 1;


Please help regarding this.
Thank you.

Reply all
Reply to author
Forward
0 new messages