Skip to content

Can't change select2 options/properties dynamically. #5071

@alihammoud90

Description

@alihammoud90

Hello,

I was trying to change the maximumSelectionLength option dynamically in multiple select2 v4.0.0 & v4.0.4 but nothing was changed.

In my code, 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 look into this issue.
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions