I came up with a solution which is a mix of the different answers posted in the select2 github page (https://github.com/select2/select2/issues/5993).
Works with more than one select2 per screen and can also be used in combination with multiple-select2.
It just gets the search input from the OPEN dropdown (.select2-container--open), which is what we're aiming at.
Yep, It's a dirty workaround, but it's the only that works for me.
let forceFocusFn = function() {
// Gets the search input of the opened select2
var searchInput = document.querySelector('.select2-container--open .select2-search__field');
// If exists
if (searchInput)
searchInput.focus(); // focus
};
// Every time a select2 is opened
$(document).on('select2:open', () => {
// We use a timeout because when a select2 is already opened and you open a new one, it has to wait to find the appropiate
setTimeout(() => forceFocusFn(), 200);
});
$('.select2Tag').select2({tags: true,width: "100%",});
) – Trinitrotoluene