Search being unfocused

Hello,

we are currently in the process of migrating from Select2 3.5 to 4.X, and noticed, that the search input loses focus straight away. I noticed the same behaviour on the official website as well, see https://select2.org/appearance#labels .

How can we fix this? Is that a bug?

Regards

I also noticed that selecting with arrow keys “works” …but it doesn’t select the current item in the dropdown.

I don’t think this is a bug, although you can certainly question the design decision.

If you use a mouse to open the dropdown (i.e., by clicking), the search box is not focused. I believe the Select2 developer assumed that you would, in most cases, use the mouse to select the item(s) from the dropdown.

Conversely, if you use the keyboard to open the dropdown (i.e., by focusing on the control and pressing Enter or Alt+DownArrow), then the search box is focused, probably under the assumption that it will be easier for a keyboard user to search for the item(s) they want to select, rather than navigating through the results with the Up/DownArrow and PageUp/Down keys).

If you want to always ensure the search box is focused when the dropdown opens, you can attach an event handler for the select2:open event. In the event handler, set focus on the search field with code like $('input.select2-search__field').[0].focus();

Hi,

i will try that… but look at the inconsistent behaviour then:

https://select2.org/getting-started/basic-usage

If you open the select2 box via mouse the search box gets focused just fine. Hence i assume this is more of a bug than deliberate design choice.

Edit: I actually tried that already and it doesn’t work wit Ajax requests. Needs a timeout before the focus. Not optimal at all.

Regards