Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 - only allow one selected AND disabled popup

I'm using the select2 plugin with the property maximumSelectionSize: 1. After selecting 1, there is an event still on the form that allows the user to click on the outside of the box and a message will appear to say: "You can only select 1 item".

I don't want this message at all. Is there a way I can ensure that this doesn't ever appear?

EDIT - code posted on request:

$('.select2-select').select2({
    maximumSelectionSize: 1
});

EDIT 2: aaand a fiddle: http://jsfiddle.net/s85k7tg7/

like image 505
user3494073 Avatar asked Jan 09 '23 02:01

user3494073


1 Answers

You could use the select2-opening event to prevent the drop-down from opening when there is a selected item.

$('.select2-select').select2({
    maximumSelectionSize: 1
}).on('select2-opening', function(e) {
    if ($(this).select2('val').length > 0) {
        e.preventDefault();
    }
});

jsfiddle

like image 126
John S Avatar answered Jan 15 '23 15:01

John S