select2 force focus on page load
Asked Answered
G

14

75

I am trying to make a select2 box appear in its focused state on page load. I have tried the following:

$('#id').select2('focus');
$('#id').trigger('click');
$('#id').trigger('focus');

Only the first line seems to have any effect, and it does focus the select2 field, however it requires an additional keypress to display the search field, and to allow typing in search string.

Therefore, if you load the page and start typing: "Search", the "S" will open the search box and then the remainder of the keys will be entered into it, so you'll be searching "earch"

Gowrie answered 20/5, 2013 at 0:39 Comment(0)
F
155

According to the Select2 documentation:

$('#id').select2('open');

Should be all you need.

Found under the Programmatic Access section in the documentation.

Flacon answered 20/5, 2013 at 0:56 Comment(3)
$('#id').select2('open'); will set focus and open the select2 ready for search. $('#id').select2('focus'); will set the focus on select2 but not select2 will be not opened for search.Phillie
As @AdrianP. said, it also opens it... And the focus option is not very visual: jsfiddle.net/mJEZK/13Caputto
Does not work with multipleConservancy
C
39

This works in release 3.4.2. Not sure when it was implemented exactly.

$('#id').select2('focus');
Concentre answered 16/9, 2013 at 17:39 Comment(2)
I couldn't find this anywhere in the documentation! Thanks a lot for sharing this!Kowloon
Works in 4.0 as well. Still undocumented.Agamogenesis
U
13

If you use:

$('#id').select2('open');

The select2 is opened and you can type directly for searching.

If you use:

$('#id').select2('open').select2('close');

The focus is set to the created select2 dropdownlist. If you hit Enter or Ctrl + Arrow down on your keyboard, you can open it.

Is personally think this is better than:

$('#id').select2('focus');

because you can't really open the select2 dropdownlist with your keyboard.

Unkenned answered 15/1, 2016 at 9:43 Comment(3)
Exactly the behavior I was looking for. Deserves more upvotes.Degas
$('#id').select2('open').select2('close'); Has the bonus of working in select2 v4.0 when nothing else seems to. Thanks!Lieabed
another solution is to directly set the focus on the created input element with $('.select2-search__field').focus();Quire
B
9

Select2 creates its own input, so try something like this:

$(window).load(function(){
  $('#id').prev('.select2-container').find('.select2-input').focus();
});
Beezer answered 20/5, 2013 at 0:55 Comment(0)
O
5

This is what worked for me, and it also placed the blinking cursor in the input field. Order matters!

$('#s2id').select2('focus');
$('#s2id').select2('open');
Overcash answered 3/6, 2016 at 2:29 Comment(0)
G
2

As described in https://forums.select2.org/t/search-being-unfocused/1203/10 focus is currently broken in the combination of Select2 4.x and JQuery 3.6.0

Two fixes: either downgrade to JQuery 3.5.1 or

// hack to fix jquery 3.6 focus security patch that bugs auto search in select-2
$(document).on('select2:open', () => {
    document.querySelector('.select2-search__field').focus();
});
Grith answered 14/11, 2021 at 5:51 Comment(1)
This is an important tip. Save my day! ThanksContreras
J
1

On Select2 4.0.2 I have a problem with select2('focus'). List looks like focused but when I press ENTER list not open.
For me that is the solution.

$('#id').select2();
$('.select2-selection', $('#id').next()).focus();
or
$('#id').next().find('.select2-selection').focus();
Julian answered 8/6, 2016 at 10:20 Comment(0)
S
1

According to the select2 documentation:

$('document').ready(function(){
   var opencustomer = $("#changedatachange").select2();
   opencustomer.select2("open");
});
Sojourn answered 18/8, 2017 at 6:57 Comment(1)
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers.Uptotheminute
O
0

I tried the other 2 answers but didn't have much luck, maybe because I populate the control via json and in the beginning it's just a hidden input so the programmatic open method didn't have any effect.

However, the following did it just fine for me:

$(document).ready(function() 
{     
    $('#s2id_autogen1').focus();
});

If for some reason you don't have the same id come up in your setup then look for the control having the select2-focusser class attached to it.

Opportuna answered 12/8, 2013 at 19:40 Comment(0)
B
0

We had a textfield as select2 and used the following snippet to activate and focus the cursor in the text input. All the other options didn't work for us, as they were only opening the select2 options, but didn't produce the expected behavior.

$('#s2id_autogen1').click()
$('#s2id_autogen1').focus()
Busybody answered 21/11, 2014 at 12:7 Comment(0)
C
0

On Select2 4.0, the method .select2('focus') does nothing. However, my workaround was simply getting the 'span' element with "aria-labelledby" attribute (notice the value is id-based, so it's kind of unique), and focus it:

var $field = $('select');
$field.select2();
var id = $field.attr('id');
var $spanLabel = $field.next().find("span[aria-labelledby='select2-" + id + "-container']");
$spanLabel.focus();
Countrywide answered 28/9, 2015 at 14:23 Comment(0)
O
0

Already well answered by Dan-Nolan but for those who are new to Select2 a little thing to note: html object needs to be intialised with select2 before calling its functions:

so final code should be

$('#id').select2();

$('#id').select2('open');
Omeromero answered 18/5, 2016 at 4:3 Comment(0)
I
-1

I found this fix elsewhere and it works for me // hack to fix jquery 3.6 focus security patch that bugs auto search in select-2

$(document).on('select2:open', () => {
    document.querySelector('.select2-container--open .select2 search__field').focus();
});

OR

$(document).on('select2:open', () => {
    document.querySelector('.select2-search__field').focus();
});
Incandesce answered 28/1, 2023 at 14:59 Comment(0)
C
-3

Use this sequece:

$('#id').select2('open');
$('#id').select2('close');
Calvo answered 14/5, 2015 at 19:42 Comment(2)
Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted. It probably doesn't help your case that this seems to diverge for no obvious reason from the long-since-accepted answer.)Onrush
If I do this, it's as if I didn't do anything: the cursor doesn't get placed into the input element created by Select2.Slur

© 2022 - 2024 — McMap. All rights reserved.