Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent select2 from autmatically focussing its search-input when dropdown is opened

I'm looking for a way to prevent select2's search-input being automatically focussed when the select2-dropdown is opened. I know this is select2's intended default behavior - and while this is fine for desktop clients, I need to prevent this behavior for the iPad where it triggers the iPads software keyboard, which is not what we want as a default.

I've searched for an option to do so with no luck. http://jsfiddle.net/KwW5n/2/ reflects our setup - we're using a simple -element as a base for our select2-functionality:

$('#source').select2();
like image 321
brckmann Avatar asked Aug 01 '13 13:08

brckmann


1 Answers

None of the solutions posted here worked for me so I did this work around:

This will make the search input readonly when opened (prevents keyboard on mobile), then when you click the input it removes readonly and opens keyboard.

$('#myselectbox').select2({placeholder: "Select Something"}).on('select2:open', function(e){
            $('.select2-search input').attr('readonly',true);
        });

$('body').on('click', '.select2-search input', function(){
            $(this).attr('readonly',false);
        });
like image 173
David B Avatar answered Jan 31 '23 00:01

David B