javascriptjquery-select2jquery-select2-3

select2 maximumSelectionSize not working after removing element


I m using select2 plugin Version: 3.4.4 My problem with maximumSelectionSize of select2. i want to allow add custom tag to user so i used select2 with tags my code is

$('#color_group').select2({
    theme: "bootstrap",
    placeholder: "Select color Name",
    tags: ['red', 'blue'],
    maximumSelectionSize: 1
});

its working fine its allows user to add only 1 color but when user remove that color try to add some other color its say You can only select 1 item i don't know whats wrong with select2. and tags are dynamically data


Solution

  • try update again value of maximumSelectionSize in select2 like this

       $('#color_group').select2({
            theme: "bootstrap",
            placeholder: "Select color Name",
            tags: ['red', 'blue'],
            maximumSelectionSize: 1
        });
    
    
       //add this code after above code and try    
        $('#color_group').on('change', function() {
                $('#color_group').select2({
                    theme: "bootstrap",
                    placeholder: "Select Group Name",
                    tags: ['red', 'blue'],
                    maximumSelectionSize: 1
                });
            });