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

11

12

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();
Anisotropic answered 1/8, 2013 at 13:12 Comment(0)
B
12

This worked for me on select2 v4:

// keep search input, but avoid autofocus on dropdown open
$('#research .filter').on('select2:open', function (e) {
    $('.select2-search input').prop('focus',false);
});

credit goes to this github comment

Breadbasket answered 25/9, 2015 at 23:12 Comment(1)
This solution seems to get Chrome crazy, this is not working and I get an error in console: jquery.js:4220 Uncaught RangeError: Maximum call stack size exceededEllipsis
G
1

Ok, I am not sure if changing the focus is possible unless you change the select2 script itself (I could be wrong about this though). As a workaround what you could do is hide the search box by setting minimumResultsForSearch property to a negative value.

<select id="test">
  <option>1</option>
  <option>2</option>    
</select>

And then:

$(document).ready(function() {
   $('#test').select2({
      minimumResultsForSearch: -1
   }); 
});

Fiddle

Gourley answered 1/8, 2013 at 13:17 Comment(3)
We're using select2 to turn a native <select>-element into a select2-dropdown; nothing else is happening, so our code exactly matches the "Basic" example at ivaynberg.github.io/select2/#basics I'll prepare a JSFiddle with that code, give me a minute please. For now I'm wondering if "focussing something else" would prevent the default behavior - select2 focussing its search-input.Anisotropic
Updated the question with a link to JSFiddle.Anisotropic
jsfiddle.net/KwW5n/3 contains your proposed code, but as expected select2's search input still gets automatically focussed - which is what we don't want to happen.Anisotropic
B
1

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);
        });
Betide answered 3/2, 2020 at 11:32 Comment(1)
Thank you @David B, most of the solutions i found where based on removing focus after the dropdown was opened. it was no use for me as in android devices the keyboard still pops up for a fraction of a second. but your solution didn't have that issue. Thanks.Perdition
Y
1

Sometimes select2 "steals" focus from other elements. After messing around for quite a bit, I just used this solution below.

At the very end of the event handler for the YourSelect2.on('change', function(){

setTimeout(firstInputFocus, 300);

}

function firstInputFocus() {

    $("YourSelect2").focus();

}

By setting this slight delay it works. I am able to change focus away from the dropdown. Following the "change" event for select2, it does something internal to the select2 code which prevents you from IMMEDIATELY changing focus. Inserting this slight delay did the trick for me at any rate.

Yeta answered 7/7, 2020 at 18:22 Comment(0)
K
0

The only 'solution' I found is to remove .select2-input and .select2-focusser right after creation of the dropdown. This only works fine when you don't need the input field for searching, e.g. when the list is short enough.

Removing only .select2-focusser at least prevents the keyboard from popping up when an option was selected.

Kleist answered 19/11, 2013 at 8:54 Comment(0)
L
0

If you want to disable the searchbox and also the auto focus as a text input, e.g. preventing ios browsers to scroll-in the keyboard, use this code:

$('select').select2({});
// will remove the searchbox and focus initially
$(".select2-search, .select2-focusser").remove();
// will remove the searchbox and focus on selection/close
$('select').on('select2:closing', function (e) {
  $(".select2-search, .select2-focusser").remove();
});
Lilt answered 16/4, 2018 at 12:46 Comment(0)
A
0

Although @Choma's answer is fine, it will alter the select2 default behavior on both desktop and mobile devices.

I had to find a solution for a responsive website: prevent the auto-focus of the search input only on mobile devices, and keep the default behaviour on desktops.

In order to detect the mobile devices, I've used Modernizr library, which can test for the existence of Touch Events in the browser.

We can use Modernizr.touch on Modenizr v2, which will return true if touch events are supported, or false otherwise.

So we can modify @Choma's answer like this:

$('select').on('select2:open', function() {
  if (Modernizr.touch) {
    $('.select2-search__field').prop('focus', false);
  }
});

Demo:

https://codepen.io/andreivictor/full/QmKxOw/

Tested on:

  • Desktop: IE 11, Chrome, Firefox, Opera, Safari
  • Android 4.2.2
  • Android 5.0.1 (Samsung Galaxy S4)
  • Android 6.0.1 (Samsung Galaxy S7 Edge)
  • iOS 11.2.5 (iPhone 8)
  • iOS 10.3.2 (iPhone 6 Plus)
  • iOS 10.3.2 (iPad Mini 3)
Ajay answered 28/4, 2018 at 11:12 Comment(0)
G
0

I got JQuery's "too much recursion" error in the console when using Choma's solution.

The following worked for me for v4:

// keep search input available, but avoid autofocus and thus mobile 
// keyboard appearing when dropdown opens.
$('body').on('select2:open','#subject', function (e) {
    $('#modal .select2-search input').attr('readonly',true);
    $('#modal .select2-search input').click(function(ev){
        $('#modal .select2-search input').attr('readonly',false);
    });
});

As you can tell this select2 field is on a modal with the id modal and the select2 field itself has an id of subject. Of course change the selector to what's appropriate for your own code.

It basically adds a readonly attribute to the input when the select2 field opens preventing a mobile keyboard from appearing, and then removes it when the search field is clicked/pressed on allowing the keyboard to appear only then.

Gauntlet answered 23/10, 2019 at 1:28 Comment(0)
H
0

Following trick worked for me. You can disable input search field of select2 element :

$('select').on('select2:opening', function() {
  $('.select2-search__field').attr("autocomplete", "new-password");
});

setTimeout(function(){ $('.select2-search__field').attr("autocomplete", "new-password"); }, 2000);
Hallette answered 1/5, 2020 at 8:58 Comment(0)
P
0

maybe someone need~ I've tried this and it works~

$('#selectID').on('select2:opening', function (e) {
                e.preventDefault();
            });
Portingale answered 15/12, 2020 at 5:24 Comment(1)
For me it causes the select box not to open!Rains
M
0

The solution worked perfectly for me. tested on mobile

// prevent auto-focus on select2 search input
    $('select').on('select2:opening', function(e) {
        $('.select2-search input').prop('focus', 1);
    });
Montevideo answered 11/9, 2021 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.